挑战:我有一列,其中有几行。例如,第一行是“水果名称”,第二行是“水果颜色”,它会重复另一个水果。我想抓住第二行(水果色)并创建一个新列。在原始列中,仅保留水果名称。
library(tidyverse)
df_before <- tribble(~Singlecolumn,"Apple","Red","Banana","Yellow","Kiwi","Grey","Grapes","Green")
df_before
Singlecolumn
<chr>
Apple
Red
Banana
Yellow
Kiwi
Grey
Grapes
Green
#I would like to split this like below:
df_after <- tribble(~Column1, ~Column2, "Apple","Red","Banana","Yellow","Kiwi","Grey","Grapes","Green")
df_after
Column1 Column2
Apple Red
Banana Yellow
Kiwi Grey
Grapes Green
我确信可以使用tidyverse中的函数来完成此操作,但是通过大量搜索无法找到任何信息。将不胜感激任何指针。预先感谢!
更容易的选择是转换为具有两列的matrix
并转换为data.frame
中的base R
as.data.frame(matrix(df_before$Singlecolumn, ncol = 2, byrow = TRUE))
但是,我们也可以使用tidyverse
,在其中我们用rep
创建两个组,然后使用pivot_wider
将形状从“长”变形为“宽”格式
library(dplyr)
library(tidyr)
df_before %>%
group_by(grp = str_c('Column', rep(1:2, length.out = n()))) %>%
mutate(rn = row_number()) %>%
ungroup %>%
pivot_wider(names_from = grp, values_from = Singlecolumn) %>%
select(-rn)
# A tibble: 4 x 2
# Column1 Column2
# <chr> <chr>
#1 Apple Red
#2 Banana Yellow
#3 Kiwi Grey
#4 Grapes Green