简体   繁体   中英

Converting Long to Wide dataset in R with multiple factors/columns

I have already converted this data set from long to wide but have lost the code! I remember it consisting of just one line. When I now look at how to convert long to wide it looks extremely more complex.

In the picture 1 you can see the data set in long form. I want to change to wide form over the variables ObstNamePOST to ReactionTimePost with C_RT as the depend variable (value). The other picture 2 shows how the data looked when I successfully did it the first time. The difference now is that I want to include one more variable in the spreading which is the ReacTimePOST.

I've tried this code

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

With no success, the 'obstNamePOST:ReacTimePost' was not recognised.

Thanks for reading and any advice you may leave....

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"))

Copied and Pasted:

Current dataset:

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

Desired dataset but I would like to add the ReacTimePOST variable:

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

I think you need to unite your columns before you spread them:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM