问题描述
我有以下数据集
我希望单元格 [2,6] 保留其当前内容,并在其下方添加单元格,以“,”分隔,我发现 paste()
函数用于将列连接成新列但找不到特定单元格组合的答案。任何帮助表示赞赏。
例如,我希望上面突出显示的单元格为John,Mary
解决方法
这是一种如何将值与下一行中的值粘贴在一起的方法。
# Loading required libraries
library(dplyr)
# Creating sample data
example <- data.frame(Type = c("Director",NA_character_),Name = c("John","Mary"))
example %>%
# Get value of the below row
mutate(new_col = lead(Name,1),# If Type is director then paste names else null
new_col = ifelse(Type == "Director",paste(Name,new_col,sep = ","),NA_character_))