6

One of the most frustrating things about R is the difficulty of creating new dataframe variables using names, algorithmically and intuitively.

Suppose I have a dataframe with some variables, and want to create new variables based on them in a loop. For example, I want to create new variables which are the cumulative sum of existing variables, and named df$var_cumul

temp<-as.data.frame(cbind(seq(0:10),seq(10:20)))
names(temp)<-c("x","y")
for (i in 1:ncol(temp)) {
  vname<-names(temp)[i]
  assign(paste("temp$",vname,"_cumul",sep=""),cumsum(contrs[,i]))
}

No permuation of that I've found works. This is probably one of my biggest issue with R on a regular basis.

Is there an easy intuitive way to do this?

6
  • 2
    You need to study the help page for "[[" more carefully. The key is avoidance of the futile effort to make $<- do something it cannot do. It would also help if you unlearned the abomination of as.data.frame(cbind(...)) Commented Jan 14, 2013 at 18:47
  • 1
    .. also never ever use 1:ncol, 1:nrow or 1:length ... (it doesn't do what you think it does and such cases are a pain to debug ...) - you want to use seq_len() and friends instead. Commented Jan 14, 2013 at 18:55
  • With DWin's second point, I believe he's referring to the fact that data.frame(x = 1:11, y = 1:11) is (among other things) a whole lot less typing. I would seriously recommend playing around with the output of things like seq(10:20) because that's pretty horrible too. (Reading ?seq would be a good start.) Commented Jan 14, 2013 at 19:05
  • My point was that cbind coerces to a matrix, which removes all attributes and makes all columns the "lowest possible denominator" generally a character matrix. Commented Jan 14, 2013 at 19:08
  • DWin - I'd say that I'd need to find the "[[' page first! It didn't come up in any search or reference about achieving this, and I'm sorry to say I'm not going to read every reference manual page. Hopefully I'll be able to reference this from now on. Much thanks! Commented Jan 14, 2013 at 19:09

1 Answer 1

7

Yes, the [[ operator:

temp <- data.frame(x = 0:10, y = 10:20)

for (n in names(temp))
  temp[[paste0(n, "_cumsum")]] <- cumsum(temp[[n]])
Sign up to request clarification or add additional context in comments.

1 Comment

If I want to create 3 dataframe can I create it in a for loop? my case is for(i in 1:3){ df.i <- data.frame(something) }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.