[R] Keep rows in a dataset if one value in a column is duplicated

Rui Barradas ruipbarradas at sapo.pt
Thu Sep 27 22:33:59 CEST 2012


Hello, again.

There was another error in the line in question. TRUE does not need 
quotes. In fact, with quotes you're comparing to a character string, not 
to a logical value.
And the other tip still holds, use as follows in the complete and 
corrected line below.

Health2PairsOnly <- PairIDs[ which(PairIDs[, "Pairiddups"] == TRUE), ]

Hope this helps,

Rui Barradas
Em 27-09-2012 20:46, GradStudentDD escreveu:
> Hi,
>
> I have a data set of observations by either one person or a pair of people.
> I want to only keep the pair observations, and was using the code below
> until it gave me the error " $ operator is invalid for atomic vectors". I am
> just beginning to learn R, so I apologize if the code is really rough.
>
> Basically I want to keep all the rows in the data set for which the value of
> "Pairiddups" is TRUE. How do I do it? And how do I get past the error?
>
> Thank you so much,
> Diana
>
> PairID<-c(Health2$pairid)
>
> duplicated(PairID, incomparables=TRUE, fromLast=TRUE)
>
> PairIDdup=duplicated(PairID)
> cbind(PairID, PairIDdup)
> PairID[which(PairIDdup)]
>
> PairIDDuplicates<-PairID%in%PairID[which(PairIDdup)]
> PairIDs<-cbind(PairID, PairIDDuplicates)
>
> colnames(PairIDs)<-c("Pairid","Pairiddups")
>
> Health2PairsOnly<-PairIDs[ which(PairIDs$Pairiddups=='TRUE'), ]
>
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/Keep-rows-in-a-dataset-if-one-value-in-a-column-is-duplicated-tp4644420.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.




More information about the R-help mailing list