比较R中的数据

时间:2017-10-05 16:01:51

标签: r dplyr

我还是R的新人,并且觉得必须有更好的方法来完成我所做的事情。我试图比较一个过程,并确定它是否适合特定的序列....此外,我后来计划扩展它,如果序列A,那么"酷",否则如果序列然后"有点酷",否则,"根本不冷静"。

对于样本数据,让我们确定面包师是否遵循烘焙食谱的正确步骤。

this.myDiv.nativeElement.classList.add('done')

连接并消除重复

我实际上需要这些代码的帮助。期望的结果是两列数据框

merged_data <-(sampledata,proper_sequence, "sequence description")

 1. Baker    Actual_Sequence_#   Sequence     proper sequence
 3. John        1         Bought ingredients    1
 4. John        2         Read recipe           1
 5. Jack        1         Read recipe           1
 6. Jack        2         Bought ingredients    1
 7. Jack        3         Mixed ingredients     3
 8. Jack        4         Preheated oven        2
 9. Jane        1         Preheated oven        2
 10. Jane       2         Bought ingredients    1
 11. Jill       1         Mixed ingredients     2



#spread the data by actual sequence and fill with proper sequence; I feel this step could be cut out, but not sure how.

spread_data<- spread(sampledata,key = "Actual_Sequence_#",value = "proper sequence")

1. Baker     1   2   3   4
2. John      1   1      
3. Jack      1   1   3   2
4. Jane      2   1      
5. Jill      2

添加一个新列,用于评估连接的实际序列和正确的序列

condensed_data<- spread_data(group_by(Baker),????)

1. Baker Sequence  concactenated 
2. John      1      
3. Jack      1,3,2
4. Jane      2,1      
5. Jill      2

1 个答案:

答案 0 :(得分:0)

library(dplyr)

#Create data.frame that looks roughly like yours
merged_data <- data.frame(Baker = c("John", "John", "Jack", "Jack", "Jack", "Jack", "Jane", "Jane", "Jill"), 
                 Actual_Sequence = c(1,2,1,2,3,4,1,2,1), 
                 proper_sequence = c(1,1,1,1,3,2,2,1,2)) 

#Use dplyr to group by baker, concatenate their process, then evaluate
#by comparing to the proper sequence field. If equal assume correct.
merged_data %>% 
  group_by(Baker) %>% 
  summarise(Actual_Sequence = paste(Actual_Sequence, collapse = ","),
            proper_sequence = paste(proper_sequence, collapse = ",")) %>%
  mutate(evaluation = ifelse(Actual_Sequence == proper_sequence, "following proper sequence", "breaking proper sequence"))

如果我理解你的帖子,我不确定,这会给你想要的结果。您可以使用dplyr语句来弄清楚它是如何工作的。