I have a data frame in R and I would like to create new columns within a for loop. I have tried many things for the last 2 days but without success. At the end, I did find a solution that seems to work, but it doesn't seem very straight forward. I was wondering if anybody has a more elegant way to do this.
Sorry if this has already been addressed but I couldn't find similar question on SO
Here is my example.
x <- runif(20)
a <- as.data.frame(x)
for (i in 1:100){
d <- x + i
a <- cbind(a, d)
}
c <- 1
for (i in 1:100){
c[i] <- paste0("colum", i)
}
colnames(a) <- c("x", c)
Thanks in advance for any good suggestions to make this process all within one loop instead of 2.
paste0("colum", 1:100)gives the same result.