0

i've a data frame with 253 columns, and 100000 + rows.

I want to make a subset of 2 columns, always keeping the first one, and summing 1 to take the next column from the right.

First loop: df[,c(1,2)]
Second loop: df[,c(1,3)]
....
Last loop: df[,c(1,253)]

I 've this code:

for(i in 2:length(colnames(df))){
    paste("a",i,sep ="") <- df[,c(1,i)]
    i += 1
}

But get this error:

> for(i in 2:length(colnames(df))){
+     paste("a",i,sep ="") <- df[,c(1,i)]
+     i += 1
Error: unexpected '=' in:
"    paste("a",i,sep ="") <- df[,c(1,i)]
    i +="
> }
Error: unexpected '}' in "}"

Note: I'm calling the new data frames a2, a3, ... Actually, i would like to name them, with the name of the corresponding colum. For example, TVs <- df[,c(1,2)] instead of: a2 <- df[,c(1,2)]

**UPDATE:

After changing i += 1 to i = i + 1, i get this error:

Error in paste("a", i, sep = "") <- df[, c(1, i)] : 
  target of assignment expands to non-language object

**UPDATE 2:

Based on comment i've used:

for(i in 2:length(colnames(df))){
    name <- paste("a",i,sep ="")
    name <- df[,c(1,i)]
    i = i + 1
}

But got just one data frame:

for(i in 2:length(colnames(df))){
    name <- paste("a",i,sep ="")
    assign(name) <- df[,c(1,i)]
    i = i + 1
}

But just got thi error:

Error in assign(name) <- df[, c(1, i)] : 
  could not find function "assign<-"
6
  • possible duplicate of how to assign values to dynamic names variables in R Commented Feb 20, 2015 at 22:09
  • do not do that; put it in a list: lapply(2:ncol(df), function(i) df[, c(1,i)]) Commented Feb 20, 2015 at 22:32
  • @eddi, may you elaborate on this, please? Make it a full answer, please. Commented Feb 20, 2015 at 22:36
  • this issue has been rehashed many times already, including in the above question linked by @Metrics and you'll find a ton of info if you just search a little Commented Feb 20, 2015 at 22:45
  • here: stackoverflow.com/q/17559390/817778 Commented Feb 20, 2015 at 22:51

1 Answer 1

-1

Based on @eddi answers, i arreived to this:

First answer:

for(i in 2:ncol(df)){
    assign(paste("a", i, sep=''), df[,c(1,i)])
}

But, as i wanted the column name as name for every new data frame, i've changed the code a bit:

for(i in 2:ncol(df)){
    assign(paste(as.character(colnames(df[i])), i, sep=''), df[,c(1,i)])
}

Thanks @eddi, . I didn't know about the assign function.

Sign up to request clarification or add additional context in comments.

7 Comments

uhmm no, this is not based in any way on what I wrote - do not use assign
require(fortunes); fortune(236)
Yeah, i know. I'm not familiar with the apply functions. I'll make another answer, with the lapply function. Until know, when i use the lapply function, R start running, but don't make individual data frames. Any hint is welcome.
lapply makes a list (of individual data.frames if you use the code I suggested); that list will make your life much better than having individual data.frames scattered all over your global environment; please read the link I gave
@eddi, Haha, i've seen the fortune(236). Ill read the link. Thanks.
|

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.