I have a loop and I am trying to create a new column to a list of lists with a constant number that is dependent on the value j (j is a number from 1 to 12). The rest of the loop works and the previous line renames the columns of the same dataframe.
for(i in 1:length(idw.list)){
idw.output[[i]] <- list()
for(j in 1:length(idw.list[[i]])){
idw.output[[i]][[j]]= as.data.frame(idw.list[[i]][[j]])
names(idw.output[[i]][[j]])[1:3] <- c("LON", "LAT", "MAXMEAN")
(idw.output[[i]][[j]])[, "MONTH"] = j
}
}
I have tried this:
(idw.output[[i]][[j]])[, "MONTH"] = j
which leads to this error:
Error in (idw.output[[i]][[j]]) <- `*vtmp*` :
could not find function "(<-"
Right now the data looks like this:
LON LAT MAXMEAN
1 11.66847 -18.04208 30.07742
2 11.76847 -18.04208 30.07742
3 11.86847 -18.04208 30.07742
and i would like to add the column, so that in the case j=1 (first loop):
LON LAT MAXMEAN MONTH
1 11.66847 -18.04208 30.07742 1
2 11.76847 -18.04208 30.07742 1
3 11.86847 -18.04208 30.07742 1
I would appreciate any help, thank you!
(idw.output[[i]][[j]])[, "MONTH"] = jtoidw.output[[i]][[j]][, "MONTH"] = j. (Parenthesis are gone). Also this is quite horrible practice. I suggest creating a small example for example 2 idenicaldata.frameswith 3 observations, with the expected result. Likely this will yield an answer with better code.