gpt4 book ai didi

r - 分割特定元素的向量,但将连续元素视为一个

转载 作者:行者123 更新时间:2023-12-05 00:56:05 27 4
gpt4 key购买 nike

我有这样的数据:

set.seed(1)
a <- pnorm(seq(-2, 2, 0.05))
a[7] <- NA
a[19:24] <- NA
a
# [1] 0.02275013 0.02558806 0.02871656 0.03215677 0.03593032 0.04005916
# [7] NA 0.04947147 0.05479929 0.06057076 0.06680720 0.07352926
#[13] 0.08075666 0.08850799 0.09680048 0.10564977 0.11506967 0.12507194
#[19] NA NA NA NA NA NA
#[25] 0.21185540 0.22662735 0.24196365 0.25784611 0.27425312 0.29115969
#[31] 0.30853754 0.32635522 0.34457826 0.36316935 0.38208858 0.40129367
#[37] 0.42074029 0.44038231 0.46017216 0.48006119 0.50000000 0.51993881
#[43] 0.53982784 0.55961769 0.57925971 0.59870633 0.61791142 0.63683065
#[49] 0.65542174 0.67364478 0.69146246 0.70884031 0.72574688 0.74215389
#[55] 0.75803635 0.77337265 0.78814460 0.80233746 0.81593987 0.82894387
#[61] 0.84134475 0.85314094 0.86433394 0.87492806 0.88493033 0.89435023
#[67] 0.90319952 0.91149201 0.91924334 0.92647074 0.93319280 0.93942924
#[73] 0.94520071 0.95052853 0.95543454 0.95994084 0.96406968 0.96784323
#[79] 0.97128344 0.97441194 0.97724987

是否可以分割向量 a进入例如向量列表根据 NA ?一个条件是连续 NA s 应该被视为一个 NA .结果向量应该对应于这个:
a[1:6]
# [1] 0.02275013 0.02558806 0.02871656 0.03215677 0.03593032 0.04005916
a[8:18]
# [1] 0.04947147 0.05479929 0.06057076 0.06680720 0.07352926 0.08075666
# [7] 0.08850799 0.09680048 0.10564977 0.11506967 0.12507194
a[25:81]
# [1] 0.2118554 0.2266274 0.2419637 0.2578461 0.2742531 0.2911597
# [7] 0.3085375 0.3263552 0.3445783 0.3631693 0.3820886 0.4012937
#[13] 0.4207403 0.4403823 0.4601722 0.4800612 0.5000000 0.5199388
#[19] 0.5398278 0.5596177 0.5792597 0.5987063 0.6179114 0.6368307
#[25] 0.6554217 0.6736448 0.6914625 0.7088403 0.7257469 0.7421539
#[31] 0.7580363 0.7733726 0.7881446 0.8023375 0.8159399 0.8289439
#[37] 0.8413447 0.8531409 0.8643339 0.8749281 0.8849303 0.8943502
#[43] 0.9031995 0.9114920 0.9192433 0.9264707 0.9331928 0.9394292
#[49] 0.9452007 0.9505285 0.9554345 0.9599408 0.9640697 0.9678432
#[55] 0.9712834 0.9744119 0.9772499

最佳答案

使用 cumsum(is.na(x)) 创建组指标,然后 split ,只保留非缺失数据

x <- is.na(a)     
split(a[!x], cumsum(x)[!x])

关于r - 分割特定元素的向量,但将连续元素视为一个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36850768/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com