簡體   English   中英

從R中的數據框中刪除重復列組合

[英]Remove duplicates column combinations from a dataframe in R

我想從以下數據中刪除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)
}

謝謝!

duplicated()有一個data.frame的方法,它只是為這類任務而設計的:

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

在您的示例中,重復的行完全重復。 與data.frames的unique作用。

udf <- unique( my.data.frame )

至於排序...... joran剛剛發布了答案。

如果您使用重復兩次它可以工作:

> 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

要解決排序問題,請先閱讀示例數據:

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),])

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM