简体   繁体   English

dplyr,分组依据和变异-错误的结果大小

[英]dplyr, group by and mutate - wrong result size

Here is my example: 这是我的示例:

col_1 <- c('a','a','a','b','b')
col_4 <- c('h','h','k','t','t')
col_2 <- c('2015-10-10','2016-10-10','2015-10-10','2016-10-10','2016-10-10')
col_3 <- c(1,2,3,445,56)
test_df <- data.frame(col_1,col_2, col_3, col_4)


df_result <- test_df %>% group_by(.dots = c('col_1', 'col_4')) %>% mutate(result_col = max(col_3))

It produces the following error message: 它产生以下错误信息:

Error in mutate_impl(.data, dots) : 
  wrong result size (2), expected 5 or 1

Could you help me to understand what I am doing wrong? 您能帮我了解我做错了什么吗?

用这个:

df_result <- test_df %>% group_by(col_1, col_4) %>% mutate(result_col = max(col_3))

If, for some reason, you want to pass quoted column names, you'll have to use group_by_ instead of group_by . 如果出于某种原因要传递带引号的列名,则必须使用group_by_而不是group_by Note _ at the end of the function. 注意_在函数末尾。

You can use all the dplyr verbs in similar fashion. 您可以类似的方式使用所有dplyr动词。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM