Home > Enterprise >  How to transpose data strings by grouped variables?
How to transpose data strings by grouped variables?

Time:07-29

This is my very first question here! So please scuse me if I do not explain myself properly.

I have for example this data:

Id PhoneNumber
1 598632541
1 578958458
1 547817745
2 417527827
3 417527745
3 757517517

As it can be seen each Id can have either one PhoneNumber or several PhoneNumbers.

I need this information to be displayed as it is shown below.

Id PhoneNumber1 PhoneNumber2 PhoneNumber3
1 598632541 578958458 547817745
2 417527827 NA NA
3 417527745 757517517 NA

I would be very greatfull If you could help me out!

CodePudding user response:

With dplyr you can do

library(dplyr)

df <- read_table("Id    PhoneNumber
1   598632541
1   578958458
1   547817745
2   417527827
3   417527745
3   757517517")

df %>%  
  group_by(Id) %>% 
  mutate(cat = paste0("PhoneNumber", row_number())) %>% 
  pivot_wider(names_from = cat, 
              values_from = PhoneNumber)

# A tibble: 3 x 4
# Groups:   Id [3]
     Id PhoneNumber1 PhoneNumber2 PhoneNumber3
  <dbl>        <dbl>        <dbl>        <dbl>
1     1    598632541    578958458    547817745
2     2    417527827           NA           NA
3     3    417527745    757517517           NA

CodePudding user response:

You can use data.table library for speed and efficiency

library(data.table)

setDT(df)
df[ , N := paste0("PhoneNumber" , 1:.N) , by = "Id" ]
dcast(df, Id ~ N , value.var = "PhoneNumber")
  • output
   Id PhoneNumber1 PhoneNumber2 PhoneNumber3
1:  1    598632541    578958458    547817745
2:  2    417527827           NA           NA
3:  3    417527745    757517517           NA
  • Related