Delete a column in a data frame within a list

If you are tidyverse user there is an alternative solution, which utilizes map function from purrr package.

# Create same sample data as above
myList <- list(A = data.frame(ID = c("A", "A"), 
                              Test = c(1, 1), 
                              Value = 1:2), 
               B = data.frame(ID = c("B", "B", "B"), 
                              Test = c(1, 3, 5), 
                              Value = 1:3))
# Remove column by name in each element of the list
map(myList, ~ (.x %>% select(-ID)))

Assuming your list is called myList, something like this should work:

lapply(myList, function(x) { x["ID"] <- NULL; x })

Update

For a more general solution, you can also use something like this:

# Sample data
myList <- list(A = data.frame(ID = c("A", "A"), 
                              Test = c(1, 1), 
                              Value = 1:2), 
               B = data.frame(ID = c("B", "B", "B"), 
                              Test = c(1, 3, 5), 
                              Value = 1:3))
# Keep just the "ID" and "Value" columns
lapply(myList, function(x) x[(names(x) %in% c("ID", "Value"))])
# Drop the "ID" and "Value" columns
lapply(myList, function(x) x[!(names(x) %in% c("ID", "Value"))])

Tags:

List

R

Dataframe