
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Subset R Data Frame Based on String Values with OR Condition
We might want to create a subset of an R data frame using one or more values of a particular column. For example, suppose we have a data frame df that contain columns C1, C2, C3, C4, and C5 and each of these columns contain values from A to Z. If we want to select rows using values A or B in column C1 then it can be done as df[df$C1=="A"|df$C1=="B",].
Consider the below data frame −
Example
set.seed(99) x1<-rep(c("A","B","C"),times=c(8,7,5)) x2<-sample(1:9,20,replace=TRUE) df1<data.frame(x1,x2) df1
Output
x1 x2 1 A 1 2 A 6 3 A 6 4 A 5 5 A 3 6 A 2 7 A 6 8 A 4 9 B 4 10 B 4 11 B 9 12 B 2 13 B 8 14 B 6 15 B 4 16 C 4 17 C 1 18 C 7 19 C 2 20 C 7
Subsetting based on rows of column x1 that contains A or C −
Example
df1[df1$x1=="A"|df1$x1=="C",]
Output
x1 x2 1 A 1 2 A 6 3 A 6 4 A 5 5 A 3 6 A 2 7 A 6 8 A 4 16 C 4 17 C 1 18 C 7 19 C 2 20 C 7
Subsetting based on rows of column x1 that contains B or C −
Example
df1[df1$x1=="B"|df1$x1=="C",]
Output
x1 x2 9 B 4 10 B 4 11 B 9 12 B 2 13 B 8 14 B 6 15 B 4 16 C 4 17 C 1 18 C 7 19 C 2 20 C 7
Subsetting based on rows of column x1 that contains A or B −
Example
df1[df1$x1=="A"|df1$x1=="B",]
Output
x1 x2 1 A 1 2 A 6 3 A 6 4 A 5 5 A 3 6 A 2 7 A 6 8 A 4 9 B 4 10 B 4 11 B 9 12 B 2 13 B 8 14 B 6 15 B 4
Let’s have a look at another example−
Example
Party<-sample(c("Democratic","Republican","Reform","Libertarian"),20,replace=TRUE) Rate_Per<-sample(1:100,20,replace=TRUE) df2<-data.frame(Party,Rate_Per) df2
Output
Party Rate_Per 1 Libertarian 45 2 Republican 79 3 Democratic 23 4 Reform 55 5 Republican 37 6 Reform 70 7 Reform 64 8 Republican 62 9 Reform 84 10 Republican 18 11 Libertarian 4 12 Republican 74 13 Reform 11 14 Libertarian 49 15 Democratic 39 16 Libertarian 76 17 Democratic 5 18 Libertarian 81 19 Democratic 1 20 Republican 56
Subsetting based on rows of column Party that contains Republican or Democratic −
Example
df2[df2$Party=="Republican"|df2$Party=="Democratic",]
Output
Party Rate_Per 1 Republican 38 2 Republican 79 4 Democratic 85 7 Republican 29 8 Republican 45 9 Democratic 12 10 Republican 73 13 Republican 38 15 Democratic 40 16 Republican 35 19 Republican 50
Advertisements