I am using the following code to access data frames df1,df2 and df3 in a loop and rename them. This gives me an error. How do I tell R that its a data frame not a
for(i in c(df1,df2,df3)) {
colnames(data.frame(i))=c("var1","var2","var3")
}
Try using a list instead
dfl=list(df1,df2,df3)
for(i in 1:length(dfl)) {
colnames(dfl[[i]])<-c("var1","var2","var3")
}
lapplyto chnage names:lapply(list(df1, df2, df3), function(x) { names(x) <- paste0("var", 1:3); x })setNames(x, paste0("var", 1:3))and avoid returning object at end.