How to subset an R data frame based on small letters?

R ProgrammingServer Side ProgrammingProgramming

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
raja
Published on 12-Nov-2021 07:54:27
Advertisements