gpt4 book ai didi

reshape2 和宽(推算)时间变量

转载 作者:行者123 更新时间:2023-12-01 13:40:22 26 4
gpt4 key购买 nike

<分区>

我知道基于 R 的 reshape 可以转换为长格式,其中时间是根据 stub 变量名称 AB 估算的,例如:

wide = data.frame(A.2010 = c('a', 'b', 'c'),
A.2011 = c('f', 'g', 'd'),
B.2010 = c('A', 'B', 'C'),
B.2011 = c('G', 'G', 'H'),
z = runif(3),
x = runif(3))

wide
# A.2010 A.2011 B.2010 B.2011 z x
#1 a f A G 0.3626823 0.67212468
#2 b g B G 0.3726911 0.09663248
#3 c d C H 0.9807237 0.31259394

变成:

reshape(wide, direction = 'long', sep = '.',
varying = c('A.2010', 'A.2011', 'B.2010', 'B.2011'))
# z x time A B id
#1.2010 0.3626823 0.67212468 2010 a A 1
#2.2010 0.3726911 0.09663248 2010 b B 2
#3.2010 0.9807237 0.31259394 2010 c C 3
#1.2011 0.3626823 0.67212468 2011 f G 1
#2.2011 0.3726911 0.09663248 2011 g G 2
#3.2011 0.9807237 0.31259394 2011 d H 3

我可以用 reshape2::melt 完成同样的事情吗?

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