【问题标题】:Remove duplicates column combinations from a dataframe in R从 R 中的数据框中删除重复的列组合
【发布时间】:2023-03-09 06:09:01
【问题描述】:

我想从以下数据中删除 sessionid、qf 和 qn 的重复组合

               sessionid             qf        qn         city
1  9cf571c8faa67cad2aa9ff41f3a26e38     cat   biddix          fresno
2  e30f853d4e54604fd62858badb68113a   caleb     amos                
3  2ad41134cc285bcc06892fd68a471cd7  daniel  folkers                
4  2ad41134cc285bcc06892fd68a471cd7  daniel  folkers                
5  63a5e839510a647c1ff3b8aed684c2a5 charles   pierce           flint
6  691df47f2df12f14f000f9a17d1cc40e       j    franz prescott+valley
7  691df47f2df12f14f000f9a17d1cc40e       j    franz prescott+valley
8  b3a1476aa37ae4b799495256324a8d3d  carrie mascorro            brea
9  bd9f1404b313415e7e7b8769376d2705    fred  morales       las+vegas
10 b50a610292803dc302f24ae507ea853a  aurora      lee                
11 fb74940e6feb0dc61a1b4d09fcbbcb37  andrew    price       yorkville 

我以 data.frame 的形式读入数据并将其称为 mydata。 Heree 是我到目前为止的代码,但我需要知道如何首先正确地对 data.frame 进行排序。其次去掉sessionid、qf、qn的重复组合。最后在 qf 列中的直方图中绘制字符

sortDATA<-function(name)
{
#sort the code by session Id, first name, then last name
sort1.name <- name[order("sessionid","qf","qn") , ]
#create a vector of length of first names
sname<-nchar(sort1.name$qf)
hist(sname)
}

谢谢!

【问题讨论】:

  • 从@Joran 的答案末尾开始,您可以使用plot(dat$qf, las=2) 绘制一个简单的直方图。 (las 参数用于将 xlabels 旋转 90 度)。

标签: r dataframe ignore-duplicates


【解决方案1】:

duplicated() 有一个data.frames 的方法,它就是为这类任务而设计的:

df <- data.frame(a = c(1:4, 1:4), 
                 b = c(4:1, 4:1), 
                 d = LETTERS[1:8])

df[!duplicated(df[c("a", "b")]),]
#   a b d
# 1 1 4 A
# 2 2 3 B
# 3 3 2 C
# 4 4 1 D

【讨论】:

  • 如何删除这些交叉重复?可以通过重复函数来完成吗?
【解决方案2】:

在您的示例中,重复的行完全重复。 unique 适用于 data.frames。

udf <- unique( my.data.frame )

至于排序... joran 刚刚发布了答案。

【讨论】:

    【解决方案3】:

    为了解决您的排序问题,首先阅读您的示例数据:

    dat <- read.table(text = "               sessionid             qf        qn         city
    1  9cf571c8faa67cad2aa9ff41f3a26e38     cat   biddix          fresno
    2  e30f853d4e54604fd62858badb68113a   caleb     amos             NA   
    3  2ad41134cc285bcc06892fd68a471cd7  daniel  folkers             NA   
    4  2ad41134cc285bcc06892fd68a471cd7  daniel  folkers             NA   
    5  63a5e839510a647c1ff3b8aed684c2a5 charles   pierce           flint
    6  691df47f2df12f14f000f9a17d1cc40e       j    franz prescott+valley
    7  691df47f2df12f14f000f9a17d1cc40e       j    franz prescott+valley
    8  b3a1476aa37ae4b799495256324a8d3d  carrie mascorro            brea
    9  bd9f1404b313415e7e7b8769376d2705    fred  morales       las+vegas
    10 b50a610292803dc302f24ae507ea853a  aurora      lee              NA  
    11 fb74940e6feb0dc61a1b4d09fcbbcb37  andrew    price       yorkville ",sep = "",header = TRUE)
    

    然后您可以使用 plyr 中的arrange

    arrange(dat,sessionid,qf,qn)
    

    或使用基函数,

    with(dat,dat[order(sessionid,qf,qn),])
    

    【讨论】:

      【解决方案4】:

      如果您使用重复两次,它会起作用:

      > df
      
        a  b c    d
      1 1  2 A 1001
      2 2  4 B 1002
      3 3  6 B 1002
      4 4  8 C 1003
      5 5 10 D 1004
      6 6 12 D 1004
      7 7 13 E 1005
      8 8 14 E 1006
      
      > df[!(duplicated(df[c("c","d")]) | duplicated(df[c("c","d")], fromLast = TRUE)), ]
      
      a  b c    d
      1 1  2 A 1001
      4 4  8 C 1003
      7 7 13 E 1005
      8 8 14 E 1006
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-11-19
        • 2020-02-16
        • 1970-01-01
        • 1970-01-01
        • 2016-11-02
        • 2012-11-13
        • 1970-01-01
        相关资源
        最近更新 更多