使用多个因子/列将R中的Long转换为Wide数据集

时间:2018-09-21 14:21:37

标签: r

我已经将此数据集从长转换为宽,但是丢失了代码!我记得它只包含一行。现在,当我研究如何将长转换为宽时,它看起来非常复杂。

在图片1中,您可以看到长格式的数据集。我想将ObstNamePOST变量的宽形式更改为以C_RT作为因变量(值)的ReactionTimePost。另一张图片2显示了第一次成功完成数据后的外观。现在的区别是,我想在传播中包括另一个变量ReacTimePOST。

我已经尝试过此代码

wide_drive <- spread(all_drive_data, 'ObstNamePOST:ReacTimePOST','NC_RT')

未成功,无法识别“ obstNamePOST:ReacTimePost”。

感谢您阅读和您留下的任何建议。...

structure(list(UsernamePOST = c(604, 604, 604, 604, 604, 604, 
606, 606, 609, 609), ObstNamePOST = c("Barrel", "Barrel", "Barrel", 
"Barrel", "Horse", "Horse", "Barrel", "Barrel", "Barrel", "Horse"
), Obst_DistPOST = c("Far", "Near", "Near", "Near", "Near", "Near", 
"Near", "Near", "Near", "Near"), Obst_SpeedPOST = c("Slow", "Slow", 
"Slow", "Fast", "Slow", "Fast", "Slow", "Fast", "Slow", "Fast"
), HeartBeat_DistancePOST = c(-300, -300, 0, 0, 0, 0, 0, -300, 
-300, -300), ReacTimePOST = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1), 
C_RT = c(NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_), NC_RT = c(100, 
100, 100, 100, 100, 100, 100, 100, 100, 100)), row.names = c(NA, 
-10L), class = c("tbl_df", "tbl", "data.frame"))

复制并粘贴:

当前数据集:

HeartBeat_DistancePOST   ReacTimePOST   C_RT     NC_RT
604  Barrel  Far     Slow   -300    1       100
604  Barrel  Near    Slow   -300    1       100
604  Barrel  Near    Slow   0   1       100
604  Barrel  Near    Fast   0   1       100
604  Horse   Near    Slow   0   1       100
604  Horse   Near    Fast   0   1       100
606  Barrel  Near    Slow   0   1       100
606  Barrel  Near    Fast   -300    1       100

所需的数据集,但我想添加ReacTimePOST变量:

Participant_Number  Adult_Left_Normal_-300  Adult_Left_Normal_000   Adult_Right_Normal_-300 Adult_Right_Normal_000  Barrel_Far_Fast_-300    Barrel_Far_Fast_000 Barrel_Far_Slow_-300    Barrel_Far_Slow_000 Barrel_Near_Fast_-300   Barrel_Near_Fast_000    Barrel_Near_Slow_-300   Barrel_Near_Slow_000    Child_Left_Normal_-300  Child_Left_Normal_000   Child_Right_Normal_-300 Child_Right_Normal_000  Horse_Far_Fast_-300 Horse_Far_Fast_000  Horse_Far_Slow_-300 Horse_Far_Slow_000  Horse_Near_Fast_-300    Horse_Near_Fast_000 Horse_Near_Slow_-300    Horse_Near_Slow_000
601 0.388346367 0.235300697 0.03155518  0.624582933 0.110802464 0.25957337  0.08679504  0.164433292 0.284582528 0.413972863 0.207617186 0.28161927  0.13972601  0.045308433 0.304697667 0.10031129  0.157257088 0.359689348 0.116680908 0.263032534 0.19115524  0.185128782 0.505271842 0.158193964
602 0.294846867 0.388679503 0.15895842  0.221110027 0.228887962 0.503114284 0.243893454 0.39346924  0.381271356 0.120846548 0.13975677  0.319363408 0.224146543 0.343569433 0.12073006  0.29235078  0.339309712 0.362793736 0.52406006  0.28859633  0.518426446 0.148005672 0.3388977   0.336997982
603 0.174173987 0.239552833 0.234517407 0.16715495  0.27220459  0.179284668 0.107244872 0.187420646 0.051934812 0.214245614 0.1635315   0.192297354 0.072041843 0.098042807 0.243052167 0.082326267 0.198791506 0.078521728 0.216784674 0.103741438 0.091583236 0.171832274 0.302374278 0.460546944
604 0.309366873 1.046732467 0.23858644  0.594436633 0.563371266 0.22630921  0.610015855 0.267286698 0.128946947 0.650960267 0.417259213 0.141574878 0.664484667 0.418242113 0.7192688   0.482289633 0.492884625 0.700427174 0.4072052   0.39345704  0.406260173 0.764027767 0.600029025 0.54421615
605 0.309895843 0.6479008   0.6499176   0.579925533 0.381809986 0.49032592  0.75975724  0.44464418  0.38040314  0.57066642  0.439990252 0.68888845  0.121592207 0.3935903   0.7694243   0.453104667 0.55651854  0.6326355   0.419287102 0.44053956  0.747642608 0.64734956  0.28784026  0.533651724
606 0.62498475  0.340876283 0.448109933 0.803698233 0.225082393 0.612350448 0.7096863   0.72827456  0.419136025 0.769892233 0.48204268  0.626884475 0.49825669  0.996437233 0.753606167 0.745627033 0.62997436  0.7341919   0.3663559   0.6824097   0.8533996   0.271708173 0.458166508 0.54191892
607 0.192169187 0.526224767 0.437194823 0.455769867 0.29140931  0.41169432  0.338650518 0.231164566 0.479882814 0.3833084   0.504272444 0.267279054 0.37011209  0.09752402  0.353108733 0.390748353 0.177707665 0.317674248 0.2109268   0.423828128 0.314648434 0.313227836 0.174044794 0.268264778
608 0.25473659  0.2265676   0.07975769  0.7150548   0.229162594 0.172861085 0.263035584 0.092198184 0.267520126 0.253094474 0.404348766 0.18172761  0.433207173 0.281443287 0.104553227 0.36150105  0.329232798 0.283331298 0.268568432 0.265734874 0.285373688 0.300894158 0.274803175 0.43774414
609 0.700414033 0.7678426   0.490203867 0.523523967 0.46584778  0.56144714  0.55058594  0.40422974  0.45477906  0.53903502  0.4534378   0.68759468  0.565592433 0.7615255   0.6405538   0.667154967 0.50109252  0.275094598 0.62073366  0.34751892  0.518737767 0.741455075 0.527481075 0.54662476

1 个答案:

答案 0 :(得分:0)

我认为您需要先unite专栏,然后spread

wide_drive <- all_drive_data %>% 
    unite(unt, ObstNamePOST:ReacTimePOST) %>%
    spread(unt, NC_RT)