I have a list containing 12 data frames. I would like to subset/split the list after every third elements based on a sequence. And make a new data frame of the new subsetted list .
# my_list
my_list
[[1]]...........[[12]]...[[nth]]
lenght(my_list) 12
The desired outcome should be like this
df1
df2
df3
df4
df_nth
#Desired outcome
subset1_my_list
[[1]]
[[2]]
[[3]]
lenght(subset1_my_list)
3
subset2_my_list
[[4]]
[[5]]
[[6]]
lenght(subset2_my_list)
3
#subset_nth_my_list
#[[n-2]] [[n-1]] [[n]]
lenght(subset_nth_my_list )
I tried this function
my_list_fun <- function(my_list, nSubsets, n){
lst <- vector("list", length = nSubsets) to <- length(my_list)
for (i in seq_len(nSubsets)) {
subset_n <- seq(i, to, n)
lst[[i]] <- data[ subset_n ,]
}
return(lst)
}
New contributor
Alis Mohd is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.