0

I'm using R but stuck at the following problem. I made a data as simple as possible to demonstrate what I want to do. I have a data as follows:

data<-data.frame(id=c(1,1,1,1,1,2,2,2,2,2),
                 date=c(20220325,20220325,20220325,20220327,20220327,20220705,20220705,20220706,20220706,20220706),
                 base=c("wt","bmi","p","wt","wt","bmi","bmi","wt","p","bmi"),
                 value=c(75,21,25,76,77,19,18,85,23,19))

There are 3 categories in column base. I want to make these 3 categories into 3 new columns like wt,bmi,p So, my expected output looks like this:

data<-data.frame(id=c(1,1,1,2,2,2),
                 date=c(20220325,20220327,20220327,20220705,20220705,20220706),
                 wt=c(75,76,77,"","",85),
                 bmi=c(21,"","",19,18,19),
                 p=c(25,"","","","",23))
Lee
  • 369
  • 1
  • 6
  • 1
    This is in essence a simple long-to-wide reshape (with duplicates); e.g. `library(tidyverse); data %>% pivot_wider(names_from = base, values_from = value, values_fn = list) %>% unnest(cols = everything())` – Maurits Evers Sep 02 '22 at 06:17

0 Answers0