user25494
user25494

Reputation: 1371

Convert long to wide with variable number of columns

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

Answers (1)

acylam
acylam

Reputation: 18691

We can change @akrun's suggestion by spreading 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

Related Questions