简体   繁体   English

如何识别列表中的所有数据框是否具有唯一ID

[英]how to identify whether all data frame in a list has unique ID or not

I have a list of dfs.我有一个 dfs 列表。 I want to know whether there is a smart way to tell whether each df in lst has unique ID , and create a summary table like below"我想知道是否有一种聪明的方法来判断lst中的每个df是否具有唯一ID ,并创建一个如下所示的汇总表"

在此处输入图像描述

Sample data:样本数据:

lst<-list(structure(list(ID = c("Tom", "Jerry", "Mary"), Score = c(85, 
85, 96)), row.names = c(NA, -3L), class = c("tbl_df", "tbl", 
"data.frame")), structure(list(ID = c("Tom", "Jerry", "Mary", 
"Jerry"), Score = c(75, 65, 88, 98)), row.names = c(NA, -4L), class = c("tbl_df", 
"tbl", "data.frame")), structure(list(ID = c("Tom", "Jerry", 
"Tom"), Score = c(97, 65, 96)), row.names = c(NA, -3L), class = c("tbl_df", 
"tbl", "data.frame")))

We could loop over the list and check with n_distinct我们可以遍历list并检查n_distinct

library(dplyr)
library(stringr)
library(purrr)
map_dfr(setNames(lst, str_c("df", seq_along(lst))), 
   ~.x %>% 
   summarise(UniqueID = c("N", "Y")[1 + (n_distinct(ID) == n())]), .id= 'Data')

-output -输出

# A tibble: 3 × 2
  Data  UniqueID
  <chr> <chr>   
1 df1   Y       
2 df2   N       
3 df3   N    

In base R :base R

data.frame(Data = paste0("df", seq(lst)),
           UniqueID = ifelse(sapply(lst, \(x) length(unique(x$ID)) == nrow(x)), "Y", "N"))
  Data UniqueID
1  df1        Y
2  df2        N
3  df3        N

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何找出在 R 数据帧中是否满足特定 ID 的特定条件? - How to find out whether a certain condition for a specific ID has ever been met in an R data frame? 如何识别数据框中相对于其他数据框的唯一列? - How to identify unique columns in a data frame with respect to other data frames? 如何识别数据框中具有相同值的列? - How to identify columns in a data frame that has same value? 如何根据时间变量识别data.frame中的唯一行和唯一观察 - How to identify unique rows and unique observations in a data.frame, based on time variable R:如何识别数据框中所有组的最小值索引 - R: How to identify indices of minima of all groups in data frame 如何通过行 id 识别数据框列中的值而不是另一个数据框列中的值? - How do I Identify by row id the values in a data frame column not in another data frame column? 通过 R 中的 ID 识别数据帧中的任何 NA - Identify any NA in a data frame by ID in R 如何从R中的面板数据框中删除具有唯一ID的行? - How to delete rows with a unique ID from a panel data frame in R? 如何在 R 的面板数据框中分配唯一的国家 ID 号 - How to assign unique country ID number in panel data frame in R 数据框单元格内的列表,如何提取唯一列表? 电阻 - List inside of data frame cell, how to extract unique lists? R
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM