[R] How to get the duplicated elements from a vector?

Gustaf Rydevik gustaf.rydevik at gmail.com
Wed Oct 29 15:57:16 CET 2008


On Wed, Oct 29, 2008 at 3:45 PM, Erik Iverson <iverson at biostat.wisc.edu> wrote:
>
>
> Leon Yee wrote:
>>
>> Gustaf Rydevik wrote:
>>> Hi Leon,
>>>
>>> unique(x)
>>>
>>> or
>>>
>>> duplicated(x)
>>>
>>> should work, depending on what you want.
>>>
>>> Best,
>>>
>>> Gustaf
>>>
>>
>> Hi,
>>    Thank you all. Actually, I have a data frame or matrix, whose first
>> column is numerical values, and whose 2nd column is names.
>
> Then you have a data.frame, as matrices in R are of homogeneous type.
>
>>I need those
>> whose names repeated 3 times and get the mean of the 3 values for each
>> repeated names.
>>
>>    It sounds that I need some programming work.
>
> Yes, but not much
>
> ## BEGIN R CODE
> ## guarantees there is at least one level with exactly three elements,
> ## which your problem seems to require
> t1 <- data.frame(a = rnorm(10), b = c("D", "D", "D", sample(LETTERS[1:3], 7,
> replace = TRUE)))
>
> ## find which names have exactly three elements
> t2 <- subset(t1, b %in% names(which(table(t1$b) == 3)))
>
> ## note that the elements of the returned value depend on what was
> ## originally in your data set's 'b' column
> tapply(t2$a, t2$b, mean)
>
> ## END R CODE
>

I'm always forgetting about the "ave" function. Using that one, here's
another way:

temp<-data.frame(Num=sample(1:1000,100),Names=sample(letters[1:25],100,replace=T))
temp$count<-ave(rep(1,nrow(temp)),temp$Names,FUN=sum)
temp$MeanOfThree[temp$count==3]<-
ave(temp$Num[temp$count==3],temp$Names[temp$count==3])

/Gustaf

-- 
Gustaf Rydevik, M.Sci.
tel: +46(0)703 051 451
address:Essingetorget 40,112 66 Stockholm, SE
skype:gustaf_rydevik



More information about the R-help mailing list