Reputation: 1371
Given the following data in long form, I would like to create a wide dataset with one row for each srdr_id, and a separate column for each arm_name as below.
Desired output:
srdr_id c1 c2 c3
174212 TAU MI MI
172612 TAU MI
I've tried tidyr::spread
without success.
dat <- structure(list(srdr_id = c("174212", "174212", "174212", "172612",
"172612"), arm_name = c("TAU", "MI", "MI", "TAU", "MI")), class = c("tbl_df",
"tbl", "data.frame"), row.names = c(NA, -5L))
Following the first suggestion, I tried:
dat %>% group_by(srdr_id) %>% mutate(rn = row_number()) %>% spread(srdr_id, arm_name)
Resulting in:
m 172612 174212
1 TAU TAU
2 MI MI
3 NA MI
I would like the transposed version.
Upvotes: 1
Views: 65
Reputation: 18691
We can change @akrun's suggestion by spread
ing id
and arm_name
, as id
is the "key" and arm_name
the "value":
library(dplyr)
library(tidyr)
dat %>%
group_by(srdr_id) %>%
mutate(id = paste0("c", row_number())) %>%
spread(id, arm_name)
Output:
# A tibble: 2 x 4
# Groups: srdr_id [2]
srdr_id c1 c2 c3
<chr> <chr> <chr> <chr>
1 172612 TAU MI <NA>
2 174212 TAU MI MI
Upvotes: 1