tst <- data.frame(ID = c(123, 123, 123, 124, 124, 124),
Identifier = c("S2", "S2", "S5", "S3", "S2", "S2"),
Speed = c(15,24,11,34,13,15),
Box_loc = c(801,802,803,801,802,803),
Ord = c(86,87,90,81,82,84))
I have been attempting to flip the above data from long to wide format, however whenever i try to research it myself, the data does not look similar enough for me to be able to translate it properly. And it seems like it should be a simple solution, but maybe I am missing some critical details.
ID | Identifier(801) | Speed(801) | Ord(801) | Identifier(802) | Speed(802) | Ord(802) | Identifier(803) | Speed(803) | Ord(803) |
---|---|---|---|---|---|---|---|---|---|
123 | S2 | 15 | 86 | S2 | 24 | 87 | S5 | 11 | 90 |
124 | S3 | 34 | 81 | S2 | 13 | 82 | S2 | 15 | 84 |
I want to get my data to look like this table. The actual data has more than just 2 ID's (if that matters), and the numbers in the headers are the Box_loc. So in other words, for each Box_loc, there are IDs from 123-250 which each need their own row with these columns. My actual file has 10 columns or so, but the syntax should be about the same.
You could use the following solution:
library(tidyr)
tst %>%
pivot_wider(names_from = Box_loc,
values_from = !c(ID, Box_loc),
names_glue = "{.value}({Box_loc})")
# A tibble: 2 x 10
ID `Identifier(801)` `Identifier(802)` `Identifier(803)` `Speed(801)` `Speed(802)`
<dbl> <chr> <chr> <chr> <dbl> <dbl>
1 123 S2 S2 S5 15 24
2 124 S3 S2 S2 34 13
# ... with 4 more variables: Speed(803) <dbl>, Ord(801) <dbl>, Ord(802) <dbl>, Ord(803) <dbl>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With