R - 在非空行之间以空行顺序生成数字

时间:2016-04-06 22:30:47

标签: r

如果我有一个如此定义的数据框:

     X      Condition
NA            One
0.169358185   NA
0.94108908    NA
0.772270715   NA
0.809542856   NA
0.426230376   NA
0.54298465    NA
0.386102588   NA
0.147564719   NA
NA            Two
0.083204676   NA
0.030533656   NA
0.905891284   NA
NA            One
0.30843373    NA
0.417785805   NA
0.063145741   NA
0.328035986   NA
NA            Two
0.045242478   NA
0.64039683    NA
0.301090671   NA
0.127325708   NA

我想在Condition列中的非NA值之间依次生成数字,最终得到如下数据框:

     X      Condition
NA            One
0.169358185   1
0.94108908    2
0.772270715   3
0.809542856   4
0.426230376   5
0.54298465    6
0.386102588   7
0.147564719   8
NA            Two
0.083204676   1
0.030533656   2
0.905891284   3
NA            One
0.30843373    1
0.417785805   2
0.063145741   3
0.328035986   4

我该怎么做?如果非NA值之间的长度是恒定的,那么这将是一个简单的seq()解决方案,但事实并非如此。它在任何两个非NA值之间随机变化。

1 个答案:

答案 0 :(得分:1)

使用sequence生成一系列由dat$Condition中每个文字标签之间的距离定义的序列:

dat$new <- sequence(diff(c(which(!is.na(dat$Condition)),length(dat$Condition)+1)))-1
dat

#            X Condition new
#1          NA       One   0
#2  0.16935818      <NA>   1
#3  0.94108908      <NA>   2
#4  0.77227071      <NA>   3
#5  0.80954286      <NA>   4
#6  0.42623038      <NA>   5
#7  0.54298465      <NA>   6
#8  0.38610259      <NA>   7
#9  0.14756472      <NA>   8
#10         NA       Two   0
#11 0.08320468      <NA>   1
#12 0.03053366      <NA>   2
#13 0.90589128      <NA>   3
#14         NA       One   0
#15 0.30843373      <NA>   1
#16 0.41778581      <NA>   2
#17 0.06314574      <NA>   3
#18 0.32803599      <NA>   4
#19         NA       Two   0
#20 0.04524248      <NA>   1
#21 0.64039683      <NA>   2
#22 0.30109067      <NA>   3
#23 0.12732571      <NA>   4