答案 0 :(得分:0)
您可以使用unique
功能。
#Sample data
df <- data.frame(Statement = rep(c("good boy", "good student",
"good employee"), 3),
Comment = rep(c("positive feedback", "negative feedback",
"neutral feedback"), 3))
df
Statement Comment
1 good boy positive feedback
2 good student negative feedback
3 good employee neutral feedback
4 good boy positive feedback
5 good student negative feedback
6 good employee neutral feedback
7 good boy positive feedback
8 good student negative feedback
9 good employee neutral feedback
使用unique
功能
unique(df)
输出:
Statement Comment
1 good boy positive feedback
2 good student negative feedback
3 good employee neutral feedback
在这种情况下,我会tidyvese
包/方法。
require(tidyverse)
#Creating new sample data
df <- data.frame(Statement = c("good boy", "good student",
"good employee", "good boy", "good student",
"good employee"),
Comment = c("positive feedback", "positive feedback",
"positive feedback",
"negative feedback", "negative feedback",
"positive feedback"))
请注意,您的主要问题是您没有对评论进行编号。我按照声明安排他们,并按字母顺序编号。
df %>%
#First arrange by statment
arrange(Statement, Comment) %>%
#Group by statment and give unique IDs
group_by(Statement) %>%
mutate(CommentNumber = seq_along(Comment)) %>%
#Spread by CommnetNumber (your new "ID") and Comment
spread(CommentNumber, Comment)
最终结果:
Statement `1` `2`
<fct> <fct> <fct>
1 good boy positive feedback negative feedback
2 good employee positive feedback positive feedback
3 good student positive feedback negative feedback
答案 1 :(得分:0)
如果您想要的是安排(即排序)您的数据框(让我们称之为df
),那么您可以这样做:
library(dplyr)
arrange(df, Statement, Comment)
按Statement
然后按Comment
对数据框进行排序。