如果多个元素存储在一列中,则将行划分为多个行

  • 本文关键字:划分 一列 存储 元素 如果 r
  • 更新时间 :
  • 英文 :


我有一个类似的数据帧,只是更长:

# A tibble: 6 x 9
`First name` `Last name` Username   TwitterID    Party                             Chamber Country `Legislative per… Notes
<chr>        <chr>       <chr>      <chr>        <chr>                             <chr>   <chr>   <chr>             <chr>
1 Raynell      Andreychuk  SenAndrey… 902575098    "Conservative Party of Canada (2… Senate  Canada  1993/03/11 - 201… NA   
2 Salma        Ataullahjan SenatorSa… 498059173    "Conservative Party of Canada (2… Senate  Canada  2010/07/09 -      NA   
3 Michèle      Audette     michele_a… 948136886    "Independent Senators Group (202… Senate  Canada  2021/07/29 -      NA   
4 Denise       Batters     denisebat… 2422634642   "Conservative Party of Canada (2… Senate  Canada  2013/01/25 -      NA   
5 Diane        Bellemare   sendbelle… 1426402920   ""Canadian Senators Group (2019… Senate  Canada  2012/09/06 -      NA   
6 Wanda Thomas Bernard     SenatorWa… 83161013426… "Progressive Senate Group (2020/… Senate  Canada  2016/11/10 -      NA   

我的目标是,每当政治家活跃在一个新的政党中时,就把每一排划分成一排。我试过用ifelse()碰碰运气,但没有成功。我非常感谢能得到的任何帮助!

再现性:

structure(list(`First name` = c("Raynell", "Salma", "Michèle", 
"Denise", "Diane", "Wanda Thomas"), `Last name` = c("Andreychuk", 
"Ataullahjan", "Audette", "Batters", "Bellemare", "Bernard"), 
Username = c("SenAndreychuk", "SenatorSalma", "michele_audette", 
"denisebatters", "sendbellemare", "SenatorWanda"), TwitterID = c("902575098", 
"498059173", "948136886", "2422634642", "1426402920", "831610134262665216"
), Party = c("Conservative Party of Canada (2004/02/02 - 2019/08/13)rrnProgressive Conservative Party (1993/03/11 - 2004/02/01)", 
"Conservative Party of Canada (2010/07/09 - )", "Independent Senators Group (2021/09/27 - )rrnNon-affiliated (2021/07/29 - 2021/09/26)", 
"Conservative Party of Canada (2013/01/25 - )", ""Canadian Senators Group (2019/11/04 - )rnIndependent Senators Group (2018/02/28 - 2019/11/03)rnNon-affiliated (2018/02/15 - 2018/02/27)"", 
"Progressive Senate Group (2020/07/08 - )rrnIndependent Senators Group (2017/06/01 - 2020/07/07)rrnNon-affiliated (Independent Senators Group) (2017/03/30 - 2017/05/31)rrnNon-affiliated (2016/11/10 - 2017/03/29)"
), Chamber = c("Senate", "Senate", "Senate", "Senate", "Senate", 
"Senate"), Country = c("Canada", "Canada", "Canada", "Canada", 
"Canada", "Canada"), `Legislative period` = c("1993/03/11 - 2019/08/13", 
"2010/07/09 -", "2021/07/29 -", "2013/01/25 -", "2012/09/06 -", 
"2016/11/10 -"), Notes = c("NA", "NA", "NA", "NA", "NA", 
"NA")), row.names = c(NA, -6L), class = c("tbl_df", "tbl", 
"data.frame"))

您应该尝试tidyr::separate_rows(Party, sep = "[rn]"):

library(tidyverse)
data <-
structure(list(
`First name` = c(
"Raynell", "Salma", "Michèle",
"Denise", "Diane", "Wanda Thomas"
), `Last name` = c(
"Andreychuk",
"Ataullahjan", "Audette", "Batters", "Bellemare", "Bernard"
),
Username = c(
"SenAndreychuk", "SenatorSalma", "michele_audette",
"denisebatters", "sendbellemare", "SenatorWanda"
), TwitterID = c(
"902575098",
"498059173", "948136886", "2422634642", "1426402920", "831610134262665216"
), Party = c(
"Conservative Party of Canada (2004/02/02 - 2019/08/13)rrnProgressive Conservative Party (1993/03/11 - 2004/02/01)",
"Conservative Party of Canada (2010/07/09 - )", "Independent Senators Group (2021/09/27 - )rrnNon-affiliated (2021/07/29 - 2021/09/26)",
"Conservative Party of Canada (2013/01/25 - )", ""Canadian Senators Group (2019/11/04 - )rnIndependent Senators Group (2018/02/28 - 2019/11/03)rnNon-affiliated (2018/02/15 - 2018/02/27)"",
"Progressive Senate Group (2020/07/08 - )rrnIndependent Senators Group (2017/06/01 - 2020/07/07)rrnNon-affiliated (Independent Senators Group) (2017/03/30 - 2017/05/31)rrnNon-affiliated (2016/11/10 - 2017/03/29)"
), Chamber = c(
"Senate", "Senate", "Senate", "Senate", "Senate",
"Senate"
), Country = c(
"Canada", "Canada", "Canada", "Canada",
"Canada", "Canada"
), `Legislative period` = c(
"1993/03/11 - 2019/08/13",
"2010/07/09 -", "2021/07/29 -", "2013/01/25 -", "2012/09/06 -",
"2016/11/10 -"
), Notes = c(
"NA", "NA", "NA", "NA", "NA",
"NA"
)
), row.names = c(NA, -6L), class = c(
"tbl_df", "tbl",
"data.frame"
))
data
#> # A tibble: 6 x 9
#>   `First name` `Last name` Username   TwitterID  Party           Chamber Country
#>   <chr>        <chr>       <chr>      <chr>      <chr>           <chr>   <chr>  
#> 1 Raynell      Andreychuk  SenAndrey… 902575098  "Conservative … Senate  Canada 
#> 2 Salma        Ataullahjan SenatorSa… 498059173  "Conservative … Senate  Canada 
#> 3 Michèle      Audette     michele_a… 948136886  "Independent S… Senate  Canada 
#> 4 Denise       Batters     denisebat… 2422634642 "Conservative … Senate  Canada 
#> 5 Diane        Bellemare   sendbelle… 1426402920 ""Canadian Se… Senate  Canada 
#> 6 Wanda Thomas Bernard     SenatorWa… 831610134… "Progressive S… Senate  Canada 
#> # … with 2 more variables: Legislative period <chr>, Notes <chr>
data %>%
separate_rows(Party, sep = "[rn]") %>%
filter(Party != "")
#> # A tibble: 13 x 9
#>    `First name` `Last name` Username   TwitterID  Party          Chamber Country
#>    <chr>        <chr>       <chr>      <chr>      <chr>          <chr>   <chr>  
#>  1 Raynell      Andreychuk  SenAndrey… 902575098  "Conservative… Senate  Canada 
#>  2 Raynell      Andreychuk  SenAndrey… 902575098  "Progressive … Senate  Canada 
#>  3 Salma        Ataullahjan SenatorSa… 498059173  "Conservative… Senate  Canada 
#>  4 Michèle      Audette     michele_a… 948136886  "Independent … Senate  Canada 
#>  5 Michèle      Audette     michele_a… 948136886  "Non-affiliat… Senate  Canada 
#>  6 Denise       Batters     denisebat… 2422634642 "Conservative… Senate  Canada 
#>  7 Diane        Bellemare   sendbelle… 1426402920 ""Canadian S… Senate  Canada 
#>  8 Diane        Bellemare   sendbelle… 1426402920 "Independent … Senate  Canada 
#>  9 Diane        Bellemare   sendbelle… 1426402920 "Non-affiliat… Senate  Canada 
#> 10 Wanda Thomas Bernard     SenatorWa… 831610134… "Progressive … Senate  Canada 
#> 11 Wanda Thomas Bernard     SenatorWa… 831610134… "Independent … Senate  Canada 
#> 12 Wanda Thomas Bernard     SenatorWa… 831610134… "Non-affiliat… Senate  Canada 
#> 13 Wanda Thomas Bernard     SenatorWa… 831610134… "Non-affiliat… Senate  Canada 
#> # … with 2 more variables: Legislative period <chr>, Notes <chr>

创建于2021-11-11由reprex包(v2.0.1(

最新更新