Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have a dataframe with a list column that was created by str_extract_all(). I'm trying to identify cases where there are more than 1 unique value

#Input =
#                         List
#1:                apple,apple
#2:               apple,banana
#3: apple,orange,orange,banana``


dat<-data.table::data.table(
          List = list(c("apple","apple"),
                   c("apple","banana"),
                   c("apple","orange","orange", "banana")),
  Count_Unique = c(1L, 2L, 3L),
  Multi = c(FALSE, TRUE, TRUE)
)

I tried dplyr::mutate(Count_Unique = length(unique(List)), but this just gives me the number of unique variables for the entire dataset. I'm sure its very simple I just can't figure how to do this in a rowwise fashion using tidyverse methods if possible.

#Expected Output =
#                         List Count_Unique Multi
#1:                apple,apple            1 FALSE
#2:               apple,banana            2  TRUE
#3: apple,orange,orange,banana            3  TRUE

dat<-data.table::data.table(
          List = list(c("apple","apple"),
                   c("apple","banana"),
                   c("apple","orange","orange", "banana")),
  Count_Unique = c(1L, 2L, 3L),
  Multi = c(FALSE, TRUE, TRUE)
)
question from:https://stackoverflow.com/questions/66056193/count-unique-in-each-list-column

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
102 views
Welcome To Ask or Share your Answers For Others

1 Answer

You can use map_dbl :

library(dplyr)
library(purrr)

dat %>% mutate(Multi = map_dbl(List, n_distinct) > 1)

#                         List Count_Unique Multi
#1:                apple,apple            1 FALSE
#2:               apple,banana            2  TRUE
#3: apple,orange,orange,banana            3  TRUE

Using base R :

dat$Multi <- sapply(dat$List, function(x) length(unique(x))) > 1

Or in data.table :

library(data.table)
setDT(dat)[, Multi := sapply(List, function(x) length(unique(x))) > 1]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...