I’m wondering if there is a good way to delete multiple columns over a few different data sets in R. I have a data set that looks like:
RangeNumber Time Value Quality Approval
1 2:00 1 1 1
2 2:05 4 2 1
And I want to delete everything but the Time and Value columns in my data sets. I’m “deleting” them by setting each column to NULL, e.x.: data1$RangeNumber <- NULL.
I’m going to have upwards of 16 or more data sets with identical column setups, and data sets are going to be numbered in incremental order, e.x.: data1, data2, data3, &c.
I’m wondering if a for loop that iterates through all of the data set columns is the best way to accomplish this, or — since I have read that R is slow at for loops– if there is an easier way to do this. I’m also wondering if I need to combine all of my data sets into one variable, and then iterate through to remove the columns.
If a for loop is the best way to go, how would I set it up?
You want to gather those dataframes into a list and then run the Extract function over them. The first argument given to “[” should be TRUE so that all rows are obtained, and the second argument should be the column names (I made up three dataframes that varied in their row numbers and column names but all had ‘Time’ and ‘Value’ columns:
This is added in case the goal was to have them all together in the same dataframe:
If you are very new to R you may not have figured out that the last code did not change TimVal; it only showed what value would be returned and to make the effect durable you would need to assign to a name. Perhaps even the same name: