I have a number of data frames that all have a column with the same name; and I would like to loop over all the data frames to delete the column.
As an example :
> df1
a b
1 1 2
2 1 2
3 1 2
> df2
a c
1 1 3
2 1 3
3 1 3
I also have a list of all the data frame names :
> df.list
[[1]]
[1] "df1"
[[2]]
[1] "df2"
If I do df1$a<-NULL, column a is deleted.
I would like to delete column a in both dfs, this is what I tried :
library(dplyr)
make.null<-function(x) {
x<-NULL
}
lapply(df.list, function(x) {
get(x) %>% select(starts_with("a")) %>% make.null()
})
I also tried :
lapply(df.list, function(x) {
get(x) %>% select(-starts_with("a"))
})
The console output from the last example is
[[1]]
b
1 2
2 2
3 2
[[2]]
c
1 3
2 3
3 3
but the data frames still have a as a column :
> df1
a b
1 1 2
2 1 2
3 1 2
So, how could I get the column to be deleted in all the data frames from the list ?