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:

Troubleshooting Packet Drops in SolarFlare Onload 10G PCI Card

If you see lots of packet drops in your onload accelerated application even after going the troubleshooting discussion we did over here ,...