I have a list of data.frames and want to subset a part of the dataframes.
In this case, I want to subset the rainfall
and yield
in sublist1 and sublist2
of mainlist
and create a new list called mainlist_new
.
But I am facing this error Error in mainlist[[1]][[, 2:3]] : incorrect number of subscripts
.
Any ideas and thoughts?
here is code
and created data
> df1 <- data.frame(station = c("MADA1", "MADA2", "MADA3", "MADA4", "MADA5"),
+ rainfall = c(0, 5, 10, 15, 20),
+ yield = c(2000, 3000, 4000, 5000, 6000))
> df2 <- df1
> df3 <- df1
>
> list_1 <- list(df1, df2, df3)
>
> list_2 <- list(df1, df2, df3)
>
> mainlist <- list(list_1, list_2)
> names(mainlist) <- c("sublist1", 'sublist2')
> names(mainlist[[1]]) <- c("station", "rainfall", "yield")
> names(mainlist[[2]]) <- c("station", "rainfall", "yield")
>
> names(mainlist)
[1] "sublist1" "sublist2"
> names(mainlist[[1]])
[1] "station" "rainfall" "yield"
>
> # subset `rainfall` and `yield` is sublist1 and sublist2 and create a a new list
> mainlist_new <- list()
> mainlist_new[[1]] <- mainlist[[1]][[,2:3]]
Error in mainlist[[1]][[, 2:3]] : incorrect number of subscripts
> mainlist_new[[2]] <- mainlist[[2]][[,2:3]]
Error in mainlist[[2]][[, 2:3]] : incorrect number of subscripts
>