- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to subset an R data frame based on small letters?
If we have a data frame that contains some lower case and some upper-case string values then we might want to subset the data frame based on lower case or upper-case letters.
For this purpose, we can make use of apply and sapply function as shown in the below examples.
Example 1
Following snippet creates a sample data frame −
x1<-sample(c(letters[1:26],LETTERS[1:26]),20) x2<-sample(c(letters[1:26],LETTERS[1:26]),20) df1<-data.frame(x1,x2) df1
The following dataframe is created −
x1 x2 1 w g 2 b l 3 J Z 4 c y 5 Q M 6 T J 7 Z i 8 s c 9 r j 10 E f 11 S N 12 x R 13 n Q 14 v V 15 N n 16 X P 17 p h 18 z I 19 l x 20 P O
To subset df1 based on small letters, add the following code to the above snippet −
x1<-sample(c(letters[1:26],LETTERS[1:26]),20) x2<-sample(c(letters[1:26],LETTERS[1:26]),20) df1<-data.frame(x1,x2) df1[apply(df1==sapply(df1,tolower),1,any),]
Output
If you execute all the above given snippets as a single program, it generates the following output −
x1 x2 1 w g 2 b l 4 c y 7 Z i 8 s c 9 r j 10 E f 12 x R 13 n Q 14 v V 15 N n 17 p h 18 z I 19 l x
Example 2
Following snippet creates a sample data frame −
y1<-sample(c(letters[1:4],LETTERS[1:4]),20,replace=TRUE) y2<-sample(c(letters[1:4],LETTERS[1:4]),20,replace=TRUE) df2<-data.frame(y1,y2) df2
The following dataframe is created −
y1 y2 1 d a 2 d D 3 d b 4 d D 5 a a 6 A a 7 A b 8 A B 9 b A 10 C d 11 c B 12 D B 13 A C 14 b d 15 A B 16 c c 17 C D 18 a D 19 c a 20 d d
To subset df2 based on small letters, add the following code to the above snippet −
y1<-sample(c(letters[1:4],LETTERS[1:4]),20,replace=TRUE) y2<-sample(c(letters[1:4],LETTERS[1:4]),20,replace=TRUE) df2<-data.frame(y1,y2) df2[apply(df2==sapply(df2,tolower),1,any),]
Output
If you execute all the above given snippets as a single program, it generates the following output −
y1 y2 1 d a 2 d D 3 d b 4 d D 5 a a 6 A a 7 A b 9 b A 10 C d 11 c B 14 b d 16 c c 18 a D 19 c a 20 d d
- Related Articles
- How to subset an R data frame based on numerical and categorical column?
- How to create a subset of an R data frame based on multiple columns?
- How to subset a data frame based on a vector values in R?
- How to subset row values based on columns name in R data frame?
- How to subset an R data frame based on string values of a columns with OR condition?
- How to subset an R data frame based on string match in two columns with OR condition?
- How to subset rows of an R data frame based on duplicate values in a particular column?
- How to subset an R data frame with condition based on only one value from categorical column?
- How to subset rows based on criterion of multiple numerical columns in R data frame?
- How to subset factor columns in an R data frame?
- How to subset nth row from an R data frame?
- How to delete rows of an R data frame based on string match?
- How to create bar chart based on two groups in an R data frame?
- How to change row values based on column values in an R data frame?
- How to subset rows of an R data frame using grepl function?
