我有一个这样的数据框:
x=c("01","01","01","22","22","03","03","03","35","35")
y=c("f","f","f","m","m","m","m","m","f","f")
df=data.frame(spn=x, sex=y)
似乎:
spn sex
1 01 f
2 01 f
3 01 f
4 22 m
5 22 m
6 03 m
7 03 m
8 03 m
9 35 f
10 35 f
我想要做的是对df $ spn进行排序,让它只出现一次。适当的df $性别,如:
spn sex
1 01 f
2 03 m
3 22 m
4 35 f
我怎么能这样做?非常感谢!
答案 0 :(得分:9)
df <- df[order(df$spn), ]
> df[!duplicated(df), ]
spn sex
1 01 f
6 03 m
4 22 m
9 35 f
答案 1 :(得分:9)
df2 = df[!duplicated(df), ] # Remove duplicated rows.
df3 = df2[order(df2$spn), ] # Sort by the spn column.
df3
# spn sex
#1 01 f
#6 03 m
#4 22 m
#9 35 f
答案 2 :(得分:4)
使用唯一的订单:
df <- unique(df)
df[order(df$spn), ]
使用 dplyr , data.table :
library(dplyr)
unique(df) %>% arrange(spn)
# spn sex
# 1 01 f
# 2 03 m
# 3 22 m
# 4 35 f
library(data.table)
unique(setDT(df))[ order(spn), ]
# spn sex
# 1: 01 f
# 2: 03 m
# 3: 22 m
# 4: 35 f