compbiostats
compbiostats

Reputation: 961

Altering output format of split() in R

Suppose I have the following split:

x <- 1:5
K <- 2
y <- 5

split(sample(x), sample(1:K, y, replace = TRUE))
$`1`
[1] 3

$`2`
[1] 5 1 2 4

Is there a way to put the above into the form

      [, 1] [, 2]
 [1, ]    3     5
 [2, ]          1
 [3, ]          2
 [4, ]          4

Upvotes: 2

Views: 69

Answers (2)

MKR
MKR

Reputation: 20085

One can try tidyverse based approach as it will provide flexibility to directly use dplyr chain. The logic is to stack list in data.frame and then use tidyr::spread to change data in wide format:

library(tidyverse)

set.seed(1)

split(sample(x), sample(1:K, y, replace = TRUE))  %>% 
  stack() %>%
  group_by(ind) %>% 
  mutate(rn = row_number()) %>% 
  spread(ind, values, fill =  NA_integer_) %>%
  select(-rn) %>% as.data.frame()

#    1 2
# 1  1 2
# 2 NA 5
# 3 NA 4
# 4 NA 3

Upvotes: 0

Rich Scriven
Rich Scriven

Reputation: 99331

You could fill those empty elements with NA using length<- then it would be perfect for the result from sapply.

a <- split(sample(x), sample(1:K, y, replace = TRUE))
a
# $`1`
# [1] 2 3
#
# $`2`
# [1] 5 1 4

sapply(a, "length<-", max(lengths(a)))
#       1 2
# [1,]  2 5
# [2,]  3 1
# [3,] NA 4

Upvotes: 5

Related Questions