简体   繁体   中英

How to select columns to Pivot to Rows in a dataframe in R

I have a complex dataframe I want to pivot from wide to long. Each row has one column with the patient ID, the rest of the columns are the patient's medications, and their details (eg drug dose, unit etc.). However, all the medciations the patient is taking are all in one long row.

            patient_id <- c(1,2)
             med1_name <- c("Drug Alpha","Drug Beta")
             med1_dose <- c(50,100)
             med1_unit <- c("mg","mg")
             med1_schedule <- c("Twice a Day","Once a Day")
             med1_route <- c("Oral","Oral")
             med1_startdate <-c(07/08/2015,08/08/2015)
             med1_enddate <- c(07/08/2020,08/08/2020)
             med2_name <- c("Drug Gamma","Drug Delta")
             med2_dose <- c(125,80)
             med2_unit <- c("mg","g")
             med2_schedule <- c("When needed","Once a Day")
             med2_route <- c("Oral","Oral")
             med2_startdate <-c(07/08/2015,08/08/2015)
             med2_enddate <- c(07/08/2020,08/08/2020)
             patientmedslist <- data.frame(patient_id,med1_name,med1_dose,med1_unit,med1_schedule,med1_route,med1_startdate,med1_enddate,med2_name,med2_dose,med2_unit,med2_schedule,med2_route,med2_startdate,med2_enddate)                

I would like to pivot the contents of every 7 columns into a new row (essentially, columns 2-8 are labelled "med1_name", "med1_dose", "med1_unit" etc., with Columns 9-16 being "med2_name", "med2_dose", "med2_unit" etc.). These long column names do not change, and there are always 7 columns of data for each drug.

I have tried the following pivot_longer code:

splitter = pivot_longer (patientmedslist,
                         cols =med1_name:med100_ongoing,
                         names_to=c("name","dose","unit","schedule","route","prestudy","startdate","enddate","ongoing"),
                         names_sep = "_",
                        values_to =c("name","dose","unit","schedule","route","prestudy","startdate","enddate","ongoing")
)

And it gives me an error message about missing pieces.

Would greatly appreciate any help.

Perhaps we can use

library(tidyr)
pivot_longer(patientmedslist, cols = -patient_id, 
    names_to = c('group', '.value'), names_sep='_')

-output

# A tibble: 4 x 9
#  patient_id group name        dose unit  schedule    route startdate  enddate
#       <dbl> <chr> <chr>      <dbl> <chr> <chr>       <chr>     <dbl>    <dbl>
#1          1 med1  Drug Alpha    50 mg    Twice a Day Oral   0.000434 0.000433
#2          1 med2  Drug Gamma   125 mg    When needed Oral   0.000434 0.000433
#3          2 med1  Drug Beta    100 mg    Once a Day  Oral   0.000496 0.000495
#4          2 med2  Drug Delta    80 g     Once a Day  Oral   0.000496 0.000495

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM