Vinds
Vinds

Reputation: 35

Dataframe reshape/transpose using dplyr

I have a data frame in the below format Original format

For a given ID; the age ,sex and status remain the same.

I want to do a couple of transformations

  1. Have a single row for each "Id" and "Type".
  2. Find unique values in "Tablet" column and do a transpose .
  3. Transpose "Level" column and have the values corresponding to each tablet under newly transposed tablet column.Empty cells have to be filled with "NA".

The desired output format is attached below for reference.desired format

I have tried using dcast and reshape; tidyr with dplyr using gather and spread,however am not able to acheive the 3rd transformation

Any help would be great! Thanks

Upvotes: 0

Views: 8600

Answers (1)

Andrew Haynes
Andrew Haynes

Reputation: 2640

You can use the spread() function in the tidyr package:

Set up packages and data:

library(dplyr)
library(tidyr)


 df=data.frame(
  id=c(1,1,1,1,1,2,2),
  age=c(3,3,3,3,3,51,51),
  sex=c('f','f','f','f','f','m','m'),
  type=c('a','a','a','b','b','a','a'),
  tablet=c('t1','t2','t3','t1','t5','t3','t10'),
  level=c(1,2,3,1,4,2,3),
  status=c('n','n','n','n','n','y','y')
  )

Use group_by() to get you result by id and type.

Use spread() to transpose the data with tablet column containing the keys for the column names, and level column containing the values. spread() will fill in blanks with NA by default.

Use select() to rearrange columns into the desired format:

df %>% 
  group_by(id,type) %>% 
  spread(tablet, level) %>% 
  select(id,age,sex,type,t1,t2,t3,t5,t10,status)

Upvotes: 3

Related Questions