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


if object vector, can order of same record following method:

 > serial <- c("df12", "cv22", "ca11", "he22", "jj32", "sq11", "cv22")  > which(serial%in%serial[duplicated(serial)])  [1] 2 7 

what can if object 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 same record 102 , 143,how can line of r command?

duplicated has fromlast argument, can try like:

unique(c(which(duplicated(iris, fromlast=true)), which(duplicated(iris)))) # [1] 102 143 

the unique there in case there multiple duplicates:

serial <- c("df12", "cv22", "ca11", "he22", "jj32",              "sq11", "cv22", "cv22", "cv22") unique(c(which(duplicated(serial, fromlast=true)),           which(duplicated(serial)))) # [1] 2 7 8 9 

if wanted extract rows themselves, construct bit simpler since don't need which:

iris[duplicated(iris, fromlast=true) | duplicated(iris), ] #     sepal.length sepal.width petal.length petal.width   species # 102          5.8         2.7          5.1         1.9 virginica # 143          5.8         2.7          5.1         1.9 virginica 

Comments

Popular posts from this blog

css - Which browser returns the correct result for getBoundingClientRect of an SVG element? -

gcc - Calling fftR4() in c from assembly -

Function that returns a formatted array in VBA -