Gathering wide columns into multiple long columns using pivot_longer

前端 未结 1 1660
鱼传尺愫
鱼传尺愫 2020-12-22 11:43

I have code which converts from wide to long with gather but I have to do this column by column. I want to use pivot_longer to gather wide multiple columns with into multip

相关标签:
1条回答
  • 2020-12-22 12:30

    I have found the answer to my question:

    pivot_longer - transforms the columns in wide format starting with 'hf' and 'ac' to long format in separate columns

    names_to parameters:

    .value = contains metadata on the cell values that correspond to the original columns

    these values are pivoted in long format and added in a new columns "hf" and "ac"

    column "group" has the original column endings (e.g. the numbers 1-6) pivoted to long format

    names_pattern = regex argument specifying character "_" where column names are to be broken up

    df3 <- df %>% 
      tidyr::pivot_longer(cols = c(starts_with("hf"), starts_with("ac"), starts_with("cs"), starts_with("se")),
                          names_to = c(".value", "level"), 
                          names_pattern = "(.*)_(.*)"
    
      )
    
    0 讨论(0)
提交回复
热议问题