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