Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Split single column data frame in R at NA

Tags:

r

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.

like image 979
eschultz Avatar asked Dec 07 '25 10:12

eschultz


1 Answers

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
like image 53
agstudy Avatar answered Dec 09 '25 23:12

agstudy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!