R: Split Elements of a List into Sublists

R: split elements of a list into sublists

Use lapply:

lapply(all.inc, function(x) split(x, x$pat))

Spliting a list of lists into sublists

## Better example data
big_lst <- list(list(1:2, LETTERS[1:2]), list(3:4, LETTERS[3:4]))

## Use do.call to construct and evaluate a call that will look like:
## mapply(list, big_lst[[1]], ..., big_lst[[n]], SIMPLIFY=FALSE)
do.call(mapply, c(list, big_lst, SIMPLIFY=FALSE))
# [[1]]
# [[1]][[1]]
# [1] 1 2
#
# [[1]][[2]]
# [1] 3 4
#
#
# [[2]]
# [[2]][[1]]
# [1] "A" "B"
#
# [[2]][[2]]
# [1] "C" "D"

R splitting list into sublists by category

I am not sure I fully understood your problem but at least a partial solution should be this:

library(dplyr)

# some dummy data from your print
df <- data.frame(Q7 = c(950, 170, 490, 500, 730, 23.33, 170, 10),
ProblemGambling = c(0,0,0,0,26,27,27,27))

df %>%
# assing the groups acording to numeric range
dplyr::mutate(ProblemGambling = case_when(ProblemGambling == 0 ~ "non-problem",
ProblemGambling <= 4 ~ "low-risk",
ProblemGambling <= 7 ~ "moderate-risk",
TRUE ~ "problem")) %>%
# build groupings to be able to split by them
dplyr::group_by(ProblemGambling) %>%
# split into sublist according to grouping
dplyr::group_split()

[[1]]
# A tibble: 4 x 2
Q7 ProblemGambling
<dbl> <chr>
1 950 non-problem
2 170 non-problem
3 490 non-problem
4 500 non-problem

[[2]]
# A tibble: 4 x 2
Q7 ProblemGambling
<dbl> <chr>
1 730 problem
2 23.3 problem
3 170 problem
4 10 problem

How to split a list of vectors into sublist based on a values of another vector in r

We can use split to split the list into elements by creating groups to split.

split(x, rep(c(1, 2), y))

#$`1`
#$`1`$x1
#[1] 1 2 3

#$`1`$x2
#[1] 1 4 3

#$`1`$x3
#[1] 3 4 6

#$`2`
#$`2`$x4
#[1] 4 8 4

#$`2`$x5
#[1] 4 33 4

#$`2`$x6
#[1] 9 6 7

We can also write a function to do this

split_list <- function(x, split_var) {
split(x, rep(1:length(split_var), split_var))
}

split_list(x, y1)
#$`1`
#$`1`$x1
#[1] 1 2 3

#$`1`$x2
#[1] 1 4 3

#$`2`
#$`2`$x3
#[1] 3 4 6

#$`2`$x4
#[1] 4 8 4

#$`2`$x5
#[1] 4 33 4

#$`2`$x6
#[1] 9 6 7

R: given a list, return a list of equal-length sublists

As @joran mentioned you should have a look at ?split.
To solve your specific problem:

a <- 1:5
n <- length(a)
k <- 2 ## your LEN
split(a, rep(1:ceiling(n/k), each=k)[1:n])
#$`1`
#[1] 1 2
#
#$`2`
#[1] 3 4
#
#$`3`
#[1] 5

Split list based on rows of list items

We can get the number of rows with sapply and split based on that info

new_list <- split(full_list, sapply(full_list, nrow))
str(new_list)
#List of 3
# $ 10:List of 2
# ..$ df1: int [1:10, 1:10] 1 0 0 1 1 0 1 0 0 1 ...
# ..$ df4: int [1:10, 1:10] 1 0 1 1 1 0 0 0 1 1 ...
# $ 15:List of 1
# ..$ df2: int [1:15, 1:10] 0 1 1 0 0 0 0 0 0 1 ...
# $ 20:List of 1
# ..$ df3: int [1:20, 1:10] 1 1 0 1 0 1 1 1 0 1 ...

As it is a nested list, we can do the processing in the inner list by calling lapply inside the first lapply

traintestlst <- lapply(new_list, function(sublst) lapply(sublst, function(x_table) {

sample_vector <- sample.int(n = nrow(x_table),
size = floor(0.8 * nrow(x_table)), replace = FALSE)
train_set <- x_table[sample_vector, ]
test_set <- x_table[-sample_vector, ]
list(train_set = train_set, test_set = test_set)

})
)

-checking the output

traintestlst[[1]]$df1
#$train_set
# [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
#[1,] 1 1 0 1 0 0 1 1 1 0
#[2,] 1 0 1 1 1 0 0 0 1 0
#[3,] 0 1 0 0 1 1 0 1 1 0
#[4,] 1 1 0 1 0 0 1 0 0 1
#[5,] 0 0 0 1 0 0 1 0 1 0
#[6,] 0 1 1 0 1 0 1 0 1 0
#[7,] 1 0 1 1 0 0 0 0 0 1
#[8,] 0 1 0 0 0 1 0 0 1 0

#$test_set
# [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
#[1,] 0 0 0 0 0 1 0 1 0 1
#[2,] 1 0 0 0 0 0 0 1 1 0

Grouping elements of a list into sublists when a row is blank

You are correct that split can help you. If you cumsum a logical vector it will break apart your original vector into groups. You then have to drop the first element because it is "". That's what tail does in the lapply:

set.seed(201)
x <- sample(letters, 20, replace = T)
x[c(6,12)] <- ""

> lapply(split(x, cumsum(x == "")), tail, -1)
$`0`
[1] "p" "p" "q" "r"

$`1`
[1] "v" "n" "g" "l" "t"

$`2`
[1] "p" "p" "n" "e" "t" "c" "j" "m"


Related Topics



Leave a reply



Submit