Home > Back-end >  I am stuck in using "pivot_longer" in reshaping this dataset
I am stuck in using "pivot_longer" in reshaping this dataset

Time:11-16

df <- tibble(
  uid = c("x1","x2","x3"),
  v13y1 = c(1,3,5),
  v13y1i = c(0,1,0),
  v13y1ii = c(1,0,0),
  v13y1iii = c(0,1,1),
  v13y1iv = c(0,2,0),
  v13y1v = c(1,3,0),
  v13y2 = c(2,3,2),
  v13y2i = c(0,1,0),
  v13y2ii = c(1,0,0),
  v13y2iii = c(0,1,1),
  v13y2iv = c(0,2,0),
  v13y2v = c(1,3,0)
)

I would like to split and rename the columns based on multiple column names. Desired output data frame should have the names of the columns as follows:

df_new <- tibble(uid = c("x1","x1", "x2","x2", "x3","x3"),
                 v13y = c(1,2,3,3,5,2),
                 n1 = c(0,0,1,1,0,0),
                 n2 = c(1,1,0,0,0,0),
                 n3 = c(0,0,1,1,1,1),
                 n4 = c(0,0,2,2,0,0),
                 n5 = c(1,1,3,3,0,0))

CodePudding user response:

This may help

library(dplyr)
library(tidyr)
library(stringr)
df %>% 
    rename_with(~ str_c(., "v13y"), matches("\\d$")) %>% 
    pivot_longer(cols = -uid, names_to = ".value", 
         names_pattern = ".*y\\d (.*)$") %>% 
    rename_with(~ str_c('n', seq_along(.)), -(1:2))

-output

# A tibble: 6 × 7
  uid    v13y    n1    n2    n3    n4    n5
  <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 x1        1     0     1     0     0     1
2 x1        2     0     1     0     0     1
3 x2        3     1     0     1     2     3
4 x2        3     1     0     1     2     3
5 x3        5     0     0     1     0     0
6 x3        2     0     0     1     0     0
  • Related