1

这是我的例子:

library(dplyr)
my_df <- data.frame( col_1 = I(list(c(1,2), 1 )), col_2 = c(1,2))
my_df <- my_df %>% rowwise(diff = setdiff(col_1, col_2))

它会产生一个错误:

Error in rowwise(., diff = setdiff(col_1, col_2)) : 
  unused argument (diff = setdiff(col_1, col_2))

出了什么问题以及如何解决?有什么想法吗?

更新:我的错,我只是忘了添加mutate

my_df <- data.frame( col_1 = I(list(c(1,2), 1 )), col_2 = c(1,2))
my_df <- my_df %>% rowwise() %>% mutate(diff = setdiff(col_1, col_2))
4

1 回答 1

1

一个选项是map2两列list

library(dplyr)
library(purrr)
my_df %>%
     mutate(diff = map2(col_1, col_2, setdiff))

或者如果我们需要使用rowwise

my_df %>%
    rowwise %>% 
    mutate(diff = setdiff(col_1, col_2))
于 2020-03-30T19:11:56.283 回答