简体   繁体   English

多个数据帧中的多个相同列 - R

[英]Multiple identical columns in multiple data frames - R

I have four data frames with one similar column named "Type".我有四个data frames ,其中一个类似的列名为“类型”。

df1 <- data.frame(Type = c('A','B','C','D','E','F'))
df2 <- data.frame(Type = c('A','B','C','D','E','F'))
df3 <- data.frame(Type = c('A','B','C','D','E','F'))
df4 <- data.frame(Type = c('A','Z','C','D','E','F'))

I would like to check if this column is identical over all data frames .我想检查此列在所有data frames中是否相同。

At the moment I'm checking two data frames at a time by using this:目前,我正在使用以下方法一次检查两个数据帧:

as.integer(as.logical(identical(df1$Type, df2$Type)))

Which gives me 1 if identical and 0 if not.如果相同,则为 1,否则为 0。

I would like to perform a single line such as this:我想执行如下一行:

as.integer(as.logical(identical(df1$Type, df2$Type, df3$Type, df4$Type)))

But identical can only handle 2 columns at once.identical只能一次处理 2 列。

I also found this Testing for multiple identical columns in R , but this only applies for columns in one data frame .我还在R 中找到了针对多个相同列的测试,但这仅适用于一个data frame中的列。

You can use extract the Type column from the dataframes and compare any one element with all of them.您可以使用从数据框中提取Type列并将任何一个元素与所有元素进行比较。

list_df <- mget(paste0('df', 1:4))
#Or manually creating a list
#list_df <- list(df1, df2, df3, df4)

tmp <- lapply(list_df, `[[`, 'Type')
all(sapply(tmp, function(x) all(x == tmp[[1]])))
#[1] FALSE

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM