我希望有一个变量的数据帧,具有原始数据帧的相应唯一值(基于阈值).换句话说,如果一列的唯一值少于5个,则应该将其作为行添加到新的数据帧中. 例如,基于以下数据帧

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

预期的结果将是

enter image description here

推荐答案

你能做到的

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

R相关问答推荐

如何提高以键ID为列的表中键查找的效率?

在ggplot的注释表格中突出显示最大值

根据列表中项目的名称多次合并数据框和列表

行式dppr中的变量列名

当两个图层映射到相同的美学时,隐藏一个图层的图例值

R-按最接近午夜的时间进行筛选

从R中的对数正态分布生成随机数的正确方法

按时间顺序对不同事件进行分组

从非重叠(非滚动)周期中的最新数据向后开窗并在周期内计数

将向量元素重新排序为R中的第二个

来自程序包AFEX和amp;的类/函数和NICE_TABLE&冲突

如何平滑或忽略R中变量的微小变化?

将工作目录子文件夹中的文件批量重命名为顺序

以不同于绘图中元素的方式对GG图图例进行排序

防止正则表达式覆盖以前的语句

如何将宽格式的患者信息数据高效地转换为患者计数的时间序列?

为什么不能使用lApply在包装函数中调用子集

将美学添加到ggploy中的文本标签

如何在R中的两列以上使用联合(&U)?

我怎么才能把一盘棋变成一盘棋呢?