gpt4 book ai didi

r - 根据条件从r中的多个列中删除重复的行

转载 作者:行者123 更新时间:2023-12-04 13:33:27 26 4
gpt4 key购买 nike

我有一个数据集,我想删除在4个不同的列中具有重复信息的数据行。

foo<- data.frame(g1 = c("1","0","0","1","1"), v1 = c("7","5","4","4","3"), v2 = c("a","b","x","x","e"), y1 = c("y","c","f","f","w"), y2= c("y","y","y","f","c"), y3 = c("y","c","c","f","w"), y4= c("y","y","f","f","c"), y5=c("y","w","f","f","w"), y6=c("y","c","f","f","w"))

foo然后看起来像:
  g1 v1 v2 y1 y2 y3 y4 y5 y6
1 1 7 a y y y y y y
2 0 5 b c y c y w c
3 0 4 x f y c f f f
4 1 4 x f f f f f f
5 1 3 e w c w c w w

现在,我想删除所有基于Y1-6列重复数据的行。因此,根据所有Y变量完全相同的情况,如果正确完成,则仅删除第4行和第1行。它是多列条件。

我相信我已经接近了,但是它不能正常工作。

我试过了: new = foo[!(duplicated(foo[,1:6]))]在考虑使用重复的命令来搜索并仅找到完全匹配的命令?

我曾想过将&用作条件语句,但也无法弄清楚该怎么做。 new = foo[foo$y1==foo$y2|foo$y3|foo$y4|foo$y5|foo$y6]
我想到了哪个,但我现在不知所措,迷失了方向。我希望foo看起来像:
   g1 v1 v2 y1 y2 y3 y4 y5 y6
2 0 5 b c y c y w c
3 0 4 x f y c f f f
5 1 3 e w c w c w w

最佳答案

> foo[apply(foo[ , paste("y", 1:6, sep = "")], 1,
FUN = function(x) length(unique(x)) > 1 ), ]
g1 v1 v2 y1 y2 y3 y4 y5 y6
2 0 5 b c y c y w c
3 0 4 x f y c f f f
5 1 3 e w c w c w w

关于r - 根据条件从r中的多个列中删除重复的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12425402/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com