user1700890
user1700890

Reputation: 7730

Store setdiff results in dataframe cell

Here is my example:

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))

It generates an error:

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

What is wrong and how to fix it? Any thoughts?

Update: my bad, I simply forgot to add 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))

Upvotes: 1

Views: 115

Answers (1)

akrun
akrun

Reputation: 887158

An option is map2 as both columns are list

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

Or if we need to use rowwise

my_df %>%
    rowwise %>% 
    mutate(diff = setdiff(col_1, col_2))

Upvotes: 1

Related Questions