Friday, 9 August 2013

How to get the order number of same record if the object is data.frame?

How to get the order number of same record if the object is data.frame?

If the object is a vector, i can get the order of the same record with the
following method:
> serial <- c("df12", "cv22", "ca11", "he22", "jj32", "sq11", "cv22")
> which(serial%in%serial[duplicated(serial)])
[1] 2 7
What can i do if the object is a data.frame?
which(iris%in%iris[duplicated(iris)])
Error in `[.data.frame`(iris, duplicated(iris)) :
undefined columns selected
> which(duplicated(iris))
[1] 143
> iris[143,]
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
143 5.8 2.7 5.1 1.9 virginica
> iris[which(iris[,1]==5.8),]
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
15 5.8 4.0 1.2 0.2 setosa
68 5.8 2.7 4.1 1.0 versicolor
83 5.8 2.7 3.9 1.2 versicolor
93 5.8 2.6 4.0 1.2 versicolor
102 5.8 2.7 5.1 1.9 virginica
115 5.8 2.8 5.1 2.4 virginica
143 5.8 2.7 5.1 1.9 virginica
The order of the same record are 102 and 143,how can i get that with a
line of R command?

No comments:

Post a Comment