Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

unique / sort in data.frame

I have a data frame like this:

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)

seems like:

   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

What I'd like to do is to sort the df$spn and let it appears only once. the appropriate df$sex as well, like:

   spn sex
1  01   f
2  03   m
3  22   m
4  35   f

How could I do that? many many thanks!

like image 970
yth Avatar asked Oct 29 '12 21:10

yth


3 Answers

df <- df[order(df$spn), ]
> df[!duplicated(df), ]
  spn sex
1  01   f
6  03   m
4  22   m
9  35   f
like image 61
Maiasaura Avatar answered Nov 20 '22 01:11

Maiasaura


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
like image 42
bdemarest Avatar answered Nov 20 '22 01:11

bdemarest


Use unique then order:

df <- unique(df)
df[order(df$spn), ]

Using 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
like image 4
zx8754 Avatar answered Nov 20 '22 02:11

zx8754