Consider
a1<-data.frame(a=c(1,2),b=c(3,4))
a2<-data.frame(a=c(5,6),b=c(7,8))
I want to rename the columns to j and k.
I did
lapply(list(a1,a2),function(ct){names(ct)<-c("j","k")})
but the old column names, a and b, persist. Can someone help?
If you have only two such dataframes, we can directly do :
names(a1) <- c("j","k")
names(a2) <- c("j","k")
If we have multiple such dataframes, we can do get them in a list using mget
, change the names with lapply
and then use list2env
to get them in global environment.
listdf <- lapply(mget(paste0("a", 1:2)), function(ct) {names(ct)<-c("j","k");ct})
list2env(listdf, .GlobalEnv)