I have a large data set that I want to split into individual units. Right now, these unit barriers are marked by NA, but how do I split them? Sample set:
df=matrix(c(1,2,3,4,NA,6,7,8,NA,10,11,12),ncol=1,byrow=TRUE)
gives us
[,1]
[1,] 1
[2,] 2
[3,] 3
[4,] 4
[5,] NA
[6,] 6
[7,] 7
[8,] 8
[9,] NA
[10,] 10
[11,] 11
[12,] 12
I would like these three stored in separate variables, such that
a
[,1]
[1,] 1
[2,] 2
[3,] 3
[4,] 4
b
[,1]
[1,] 6
[2,] 7
[3,] 8
c
[,1]
[1,] 10
[2,] 11
[3,] 12
Does this make sense? Thanks.
One line solution using split and cumsum after removing missing values:
split(df[!is.na(df)],cumsum(is.na(df))[!is.na(df)])
$`0`
[1] 1 2 3 4
$`1`
[1] 6 7 8
$`2`
[1] 10 11 12
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With