如何与R中的vector语句进行比较

时间:2018-04-30 05:21:47

标签: r

output

请帮我解决这个问题。

下面是示例

RouteAttr.RoutedForRole

在上表中我们有6个声明,我们确实有重复。现在想知道那些相同和不同评论的陈述。

提前致谢。

2 个答案:

答案 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对数据框进行排序。