How to use casting in change without aggregation

Many of the uses I've seen use an aggregation function, such as average.

How about whether you just want to change the form without losing information. For example, if I want to take this long format:

ID condition Value John a 2 John a 3 John b 4 John b 5 John a 6 John a 2 John b 1 John b 4 

In this wide format without any aggregation:

 ID ab John 2 4 John 3 5 Alex 6 1 Alex 2 4 

I assume that this assumes that the observations are paired and you have lost the value, it will ruin, but any understanding will be appreciated

+4
source share
1 answer

In such cases, you can add a serial number:

 library(reshape2) DF$seq <- with(DF, ave(Value, ID, condition, FUN = seq_along)) dcast(ID + seq ~ condition, data = DF, value.var = "Value") 

The last line gives:

  ID seq ab 1 John 1 2 4 2 John 2 3 5 3 John 3 6 1 4 John 4 2 4 

(Please note that we used the sample input from the question, but the sample output in the question does not match the sample input.)

+6
source

Source: https://habr.com/ru/post/1480291/


All Articles