I try to copy a column within a dataframe. Next I try to rename the columnname in the code below although I do not succeed.
library(dplyr)
d <- data.frame(red=1:3, green=4:6, yellow=7:9)
for (q in 1:3) {
d$color <- select(d, q)
colnames(d)[4] <- "newcolor"
}
First I try to change the name with d$newcolor which does not succeed. Next I try using colnames which does not succeed either. Although this works fine on the first 3 columns.
I need a solution with identifying the columns by their numbers since I need it as part of a loop. (I actually try to acomplish to fill the 4th column every loop with a different source column)
Thanks a lot!