我试图在R中设立两栏。 我正在讨论的问题是,根据观察类型,这一年与上一栏没有分开。
数据框架中的一些名字只是第一名字,而另一些名字是头等和最后的名字。 我正试图将<代码>Name第一和第一/最后和<编码>>年代码>与目前的<代码>Name栏分开。
Fake data = Employee and the year theystart employment
建立数据框架
dat <- tibble(Name = c("Percy Vere (2020)", "Ginger Plant (2017)", "Perry (2019)",
"Pat Thettick (2020)", "Samuel (2022)", "Fay Daway (2008)",
"Greg (2022)", "Simon Sais (2011)"))
# A tibble: 8 x 1
Name
<fct>
1 Percy Vere (2020)
2 Ginger Plant (2017)
3 Perry (2019)
4 Pat Thettick (2020)
5 Samuel (2022)
6 Fay Daway (2008)
7 Greg (2022)
8 Simon Sais (2011)
将该栏分为两栏:和>
dat %>%
select_all() %>%
separate(col = Name, into = c( Name , Year )) %>% # sep = , and ; does not create a fix
tibble()
# A tibble: 8 x 2
Name Year
<chr> <chr>
1 Percy Vere
2 Ginger Plant
3 Perry 2019
4 Pat Thettick
5 Samuel 2022
6 Fay Daway
7 Greg 2022
8 Simon Sais
Warning message:
Expected 2 pieces. Additional pieces discarded in 8 rows [1, 2, 3, 4, 5, 6, 7, 8].