Monday, February 16, 2015

R : Using grepl

Sometimes the need is for selecting and displaying all columns of a dataset except for 1 or 2 columns.
Identifying the columns by index no may not always be possible. If the data is correctly labelled and column names are aptly named, then using index number hinders the code readability. To overcome such scenario use the following code:

 > studentData <- data.frame(ID=paste0("Student",1:50),
+                           Math=sample(100,50),
+                           Science=sample(100,50),
+                           History=sample(100,50),
+                           Final=sample(100,50))
> studentData
          ID Math Science History Final
1   Student1   15      78      41    93
2   Student2   85      46      52    75
3   Student3   10      12      17    99.....

> studentData[,!grepl("ID",colnames(studentData))]
   Math Science History Final
1    15      78      41    93
2    85      46      52    75
3    10      12      17    99...
Happy programming!!

No comments:

Section 80C TMT 2023(PA) – Standard Deduction amounting to a maximum of Rs 8670 under IT rule

With the recently concluded Tax filing season (which if I may draw parallel to the Christmas holiday season enjoyed by one and all), Indians...