【发布时间】:2020-07-29 01:10:32
【问题描述】:
挑战:我有一列有几行。例如,第一行是“水果名称”,第二行是“水果颜色”,它对另一个水果重复。我想抓住每隔一行(水果颜色)并创建一个新列。在原始列中只保留水果名称
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 中的函数来执行此操作,但无法通过大量搜索找到任何信息。 将不胜感激任何指针。提前致谢!
【问题讨论】:
标签: r dplyr tidyverse tidyr purrr