Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
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 −
x1The 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 OTo subset df1 based on small letters, add the following code to the above snippet −
x1Output
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 xExample 2
Following snippet creates a sample data frame −
y1The 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 dTo subset df2 based on small letters, add the following code to the above snippet −
y1Output
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
