问题与「;setdiff";R中的命令
我有一个问题是关于我在R中使用“setdiff”命令时遇到的问题 我已使用以下命令将2个excel文件加载到R中:问题与「;setdiff";R中的命令,r,excel,R,Excel,我有一个问题是关于我在R中使用“setdiff”命令时遇到的问题 我已使用以下命令将2个excel文件加载到R中: data.x<- read_excel("c:/Users/User/Dropbox/excel til R/X.xlsx", col_names=FALSE) data.y<- read_excel("c:/Users/User/Dropbox/excel til R/Y.xlsx", col_names=FALSE) 在这里,我希望它能告诉我“data.x”中缺少
data.x<- read_excel("c:/Users/User/Dropbox/excel til R/X.xlsx", col_names=FALSE)
data.y<- read_excel("c:/Users/User/Dropbox/excel til R/Y.xlsx", col_names=FALSE)
在这里,我希望它能告诉我“data.x”中缺少哪些变量。但它只是显示“data.y”中存在的数据,就好像我只是使用命令:“data.y”
我是做错了什么还是错过了什么
任何帮助都将不胜感激 如果无法访问您问题中提到的文件,我将使用
mtcars_1 <- mtcars[-1, ][, -1]
mtcars_2 <- mtcars[-2, ][, -2]
# common column names
intersect(names(mtcars_1), names(mtcars_2))
# columns names only in mtcars_0 and not in mtcars_2
setdiff(names(mtcars_1), names(mtcars_2))
# columns only in mtcars_2 and not in mtcars_1
setdiff(names(mtcars_2), names(mtcars_1))
尝试使用
dplyr::setdiff()
而不仅仅是setdiff()
。
在base R中还有另一个
setdiff()
函数。setdiff
只在两个向量上工作,而不在表上工作(响应@Gregor的注释,它在表上工作,但我想不是你我假设的那样工作)。你的数据看起来怎么样,我猜你有几个列。因此,您可以将每个表的第一列输入到setdiff
。也许您想要setdiff(colnames(data.y)、colnames(data.x))
?@ManuelBickelsetdiff
在数据帧上运行良好,例如setdiff(mtcars,mtcars[-1])
。它将按行进行默认比较,如果有不同的列,则抛出错误。不确定你期望什么/你想要什么。你是对的,只是编辑了我的评论。谢谢。我试过:setdiff(colnames(data.y)、colnames(data.x))。但它只是说:字符(0)
mtcars_1 <- mtcars[-1, ][, -1]
mtcars_2 <- mtcars[-2, ][, -2]
# common column names
intersect(names(mtcars_1), names(mtcars_2))
# columns names only in mtcars_0 and not in mtcars_2
setdiff(names(mtcars_1), names(mtcars_2))
# columns only in mtcars_2 and not in mtcars_1
setdiff(names(mtcars_2), names(mtcars_1))
‘semi_join()’ return all rows from ‘x’ where there are matching
values in ‘y’, keeping just columns from ‘x’.
A semi join differs from an inner join because an inner join
will return one row of ‘x’ for each matching row of ‘y’, where a
semi join will never duplicate rows of ‘x’.
‘anti_join()’ return all rows from ‘x’ where there are not matching
values in ‘y’, keeping just columns from ‘x’.
dplyr::semi_join(mtcars_1, mtcars_2)
dplyr::semi_join(mtcars_2, mtcars_1)
dplyr::anti_join(mtcars_1, mtcars_2)
# cyl disp hp drat wt qsec vs am gear carb
# 1 6 160 110 3.9 2.875 17.02 0 1 4 4
dplyr::anti_join(mtcars_2, mtcars_1)
# mpg disp hp drat wt qsec vs am gear carb
# 1 21 160 110 3.9 2.62 16.46 0 1 4 4