行中的拆分值复制其他列值 R

Split values in row duplicating other colums values R

我有这样的数据集

Name    Year    Subject                                        State
Jack    2003    Math/ Sci/ Music                               MA/ AB/ XY
Sam     2004    Math/ PE                                       CA/ AB
Nicole  2005    Math/ Life Sci/ Geography                      NY/ DE/ FG

这就是我想要的输出:

Name    Year    Subject            State
Jack    2003    Math               MA
Jack    2003    Sci                AB
Jack    2003    Music              XY
Sam     2004    Math               CA
Sam     2004    PE                 AB
Nicole  2005    Math               NY
Nicole  2005    Life Sci           DE
Nicole  2005    Geography          FG

请记住,'subject' 的第一个元素对应于 'State' 的第一个元素,依此类推。我需要保留这封信件。我想我必须使用 'pivot_longer' 之类的东西,但我不是每天都使用 R,而且我不够熟练。提前致谢 :) 谢谢!!

Name <- c("Jack", "Sam", "Nicole")
Year = c(2003, 2004, 2005)
Subject = c("Math/ Sci/ Music",  "Math/ PE", "Math/ Life Sci/ Geography")
State = c("MA/ AB/ XY", "CA/ AB", "NY/ DE/ FG")

library(tidyr)
df <- data.frame(Name, Year, Subject, State)
df %>% separate_rows(Subject, State, sep = "/ ")
#> # A tibble: 8 × 4
#>   Name    Year Subject   State
#>   <chr>  <dbl> <chr>     <chr>
#> 1 Jack    2003 Math      MA   
#> 2 Jack    2003 Sci       AB   
#> 3 Jack    2003 Music     XY   
#> 4 Sam     2004 Math      CA   
#> 5 Sam     2004 PE        AB   
#> 6 Nicole  2005 Math      NY   
#> 7 Nicole  2005 Life Sci  DE   
#> 8 Nicole  2005 Geography FG

reprex package (v2.0.1)

创建于 2022-01-27

或者,在 pivot_longer 之后使用 pivot_wider

library(tidyverse)

df <- data.frame(
  stringsAsFactors = FALSE,
  Name = c("Jack", "Sam", "Nicole"),
  Year = c(2003L, 2004L, 2005L),
  Subject = c("Math/ Sci/ Music","Math/ PE",
              "Math/ Life Sci/ Geography"),
  State = c("MA/ AB/ XY", "CA/ AB", "NY/ DE/ FG")
)

df %>% 
  pivot_longer(cols = 3:4) %>% 
  pivot_wider(id_cols = c(Name, Year),  values_fn = \(x) str_split(x, "/ ")) %>% 
  unnest(everything())

#> # A tibble: 8 × 4
#>   Name    Year Subject   State
#>   <chr>  <int> <chr>     <chr>
#> 1 Jack    2003 Math      MA   
#> 2 Jack    2003 Sci       AB   
#> 3 Jack    2003 Music     XY   
#> 4 Sam     2004 Math      CA   
#> 5 Sam     2004 PE        AB   
#> 6 Nicole  2005 Math      NY   
#> 7 Nicole  2005 Life Sci  DE   
#> 8 Nicole  2005 Geography FG