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<-"
Omar Gonzales
  • 3,806
  • 10
  • 56
  • 120
  • possible duplicate of [how to assign values to dynamic names variables in R](http://stackoverflow.com/questions/14081982/how-to-assign-values-to-dynamic-names-variables-in-r) – Metrics Feb 20 '15 at 22:09
  • do not do that; put it in a list: `lapply(2:ncol(df), function(i) df[, c(1,i)])` – eddi Feb 20 '15 at 22:32
  • @eddi, may you elaborate on this, please? Make it a full answer, please. – Omar Gonzales Feb 20 '15 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 – eddi Feb 20 '15 at 22:45
  • here: http://stackoverflow.com/q/17559390/817778 – eddi Feb 20 '15 at 22:51
  • @eddi, that just have confused me. I'll investigate how to solve this within the apply family functions. For now, i'll try to use what you've posted: `lapply(2:ncol(df), function(i) df[, c(1,i)])` – Omar Gonzales Feb 20 '15 at 22:57

1 Answers1

-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.

Metrics
  • 15,172
  • 7
  • 54
  • 83
Omar Gonzales
  • 3,806
  • 10
  • 56
  • 120
  • uhmm no, this is not based in any way on what I wrote - do **not** use `assign` – eddi Feb 20 '15 at 23:09
  • 1
    `require(fortunes); fortune(236)` – eddi Feb 20 '15 at 23:09
  • 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. – Omar Gonzales Feb 20 '15 at 23:10
  • `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 Feb 20 '15 at 23:13
  • @eddi, Haha, i've seen the fortune(236). Ill read the link. Thanks. – Omar Gonzales Feb 20 '15 at 23:14
  • @eddi, using lapply i got a list. But when trying to apply complete.functions to that list (`clean_data <- lapply(df_totales, function(df_totales) df_totales[complete.cases(df_totales),])`) i got this error: `Error in View : arguments imply differing number of rows` . What should i do? – Omar Gonzales Feb 20 '15 at 23:27
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/71361/discussion-between-omar-gonzales-and-eddi). – Omar Gonzales Feb 20 '15 at 23:30