Reputation: 634
I have a list of dataframes each with the same columns. I want to drop some columns by index. How do I use a select command within a map function?
Here is my attempt:
data <- data %>% map(~ select(-c(7, 11, 13, 14)))
Any ideas on how I need to change this code?
Many thanks
Upvotes: 3
Views: 1783
Reputation: 389155
You can try :
data <- purrr::map(data, ~ . %>% dplyr::select(-c(7, 11, 13, 14)))
Or in base R :
data <- lapply(data, function(x) x[,-c(7, 11, 13, 14)])
This worked for me with dplyr > 0.8
data <- map(data, .f = list(. %>% dplyr::select(-grp)))
Upvotes: 4
Reputation: 887531
We can do this without anonymous function
data <- lapply(data, `[`, -c(7, 11, 13, 14))
Upvotes: 1