Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Dataframe with unique values of some columns

I would like to have a dataframe of variables with corresponding unique values (based on a threshold) of the original dataframe. In other words, if a column has less than 5 unique values then it should be added as a row in the new dataframe.
For example, based on the following dataframe

structure(list(id = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10), gender = c("male", 
"female", "female", "female", "female", "male", "male", "female", 
"female", "female"), ranking = c("low", "medium", "medium", "medium", 
"high", "low", "medium", "low", "low", "low"), comments = c("I was really dissapointed by the fact that there was no response", 
"I got feedback from them but I considered it a lie", "The feedback was really good and I felt convinced", 
"I was informed they will get back to me", "The feedback was appropriate to me", 
"I feel the contact person wasn't knowledgeable about the product", 
"I was told they will follow up within a week but they failed to", 
"I liked their customer service", "I was told that the issue will soon be addressed", 
"I am satisfied with the resonse they gave")), class = c("tbl_df", 
"tbl", "data.frame"), row.names = c(NA, -10L))

the desired result would be

enter image description here

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You could do

library(tidyverse)

df %>% 
  select(which(sapply(df, \(x) length(unique(x)) < 5))) %>%
  summarise(across(everything(), ~ paste(unique(.x), collapse = '; '))) %>%
  {data.frame(column = names(.), unique_values = unlist(.), row.names = NULL)}
#>    column     unique_values
#> 1  gender      male; female
#> 2 ranking low; medium; high
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading