本文介绍了分组插入字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个名为教师的数据框:
I have a dataframe called Teachers:
Teachers <- data.frame(Name = c("Mark","Mark","Peter","Richard","Clay","Clay"),
Pos = c("Coord","Teacher", "Teacher", "Teacher","Coord","Teacher"))
是否有任何R基本命令会产生以下组data.frame ??
Are there any R basic commands that would produce the following group data.frame?:
Teachers2 <- data.frame(Name = c("Mark","Peter","Richard","Clay"),
Pos = c("Coord/Teacher", "Teacher", "Teacher","Coord/Teacher"))
感谢您的帮助
推荐答案
paste
应该做到:
library(tidyverse)
Teachers2 <- Teachers %>%
group_by(Name) %>%
summarise(Pos=paste(Pos,collapse="/"))
输出:
Name Pos
1 Mark Coord/Teacher
2 Peter Teacher
3 Richard Teacher
4 Clay Coord/Teacher
这篇关于分组插入字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!