user2989290
user2989290

Reputation: 47

Using a for variable in column names to be added to a data frame in R

I have a for loop in R that I want to create 10 different variables in a data frame named rand1, rand2, rand3, etc... Here is what I tried first:

for (rep in 1:10) {
    assign(paste('alldata200814$rand', rep, sep=""), runif(nrow(alldata200814), 0, 1))
}

but that doesn't work - no error/warning message so I don't know why but when I try to submit

alldata200814$rand1

it says it is NULL. So then I changed the for loop to:

for (rep in 1:10) {
    assign(paste('rand', rep, sep=""), runif(nrow(alldata200814), 0, 1))
}

and it creates the variables rand1 - rand10, but now I want to attach them to my data frame. So I tried:

for (rep in 1:10) {
    assign(paste('rand', rep, sep=""), runif(nrow(alldata200814), 0, 1))
    alldata200814 <- cbind(alldata200814, paste('rand', rep, sep=""))
}

but that just creates columns with 'rand1', 'rand2', 'rand3', etc... in every row. Then I got really close by doing this:

for (rep in 1:10) {
    data<-assign(paste('rand', rep, sep=""), runif(nrow(alldata200814), 0, 1))
    alldata200814 <- cbind(alldata200814, data)
}

but that names all 10 columns of random numbers "data" when I want them to be "rand1", "rand2", "rand3", etc... and I'm not sure how to rename them within the loop. I previously had this programmed in 10 different lines like:

alldata200814$rand1<-runif(nrow(alldata200814), 0, 1)

but I may have to do this 100 times instead of only 10 so I need to find a better way to do this. Any help is appreciated and let me know if you need more information. Thanks!

Upvotes: 1

Views: 79

Answers (1)

joran
joran

Reputation: 173697

for (i in 1:10){
    alldata200814[,paste0("rand",i)] <- runif(nrow(alldata200814), 0, 1)
}

Stop using assign. Period. And until you're confident that you'll know when to use it, distrust anyone telling you to use it.

The other idiom that is important to know (and far preferable to anything involving assign) is that you can create objects and then modify the names after the fact. For instance,

new_col <- matrix(runif(nrow(alldata200814) * 10,0,1),ncol = 10)
alldata200814 <- cbind(alldata200814,new_col)

And now you can alter the column names in place using names(alldata200814) <- column_names. You can even use subsetting to only assign to specific column names, like this:

df <- data.frame(x = 1:5,y = 1:5)
names(df)[2] <- 'z'
> df
  x z
1 1 1
2 2 2
3 3 3
4 4 4
5 5 5

Upvotes: 3

Related Questions