[R] help with the unique function
(Ted Harding)
Ted.Harding at manchester.ac.uk
Wed May 7 19:02:12 CEST 2008
On 07-May-08 16:31:23, Erik Iverson wrote:
> ravi -
> This may get you started
>
> count.reps <- function(df) {
> hash <- do.call("paste", c(df, sep = "\r"))
> cbind(unique(df), Freq = unclass(table(hash)))
> }
>
> test <- data.frame(a = rep(1:10, 2), b = rep(1:10, 2))
> count.reps(test)
That doesn't work: It muddles the Association of the "unique"
values with the counts:
A<-sample(c(0,1),100,replace=TRUE)
B<-sample(c(0,1),100,replace=TRUE)
C<-sample(c(0,1),100,replace=TRUE)
sum((A==0)&(B==0)&(C==0)) ## [1] 26
sum((A==0)&(B==0)&(C==1)) ## [1] 9
sum((A==0)&(B==1)&(C==0)) ## [1] 10
sum((A==0)&(B==1)&(C==1)) ## [1] 7
sum((A==1)&(B==0)&(C==0)) ## [1] 15
sum((A==1)&(B==0)&(C==1)) ## [1] 11
sum((A==1)&(B==1)&(C==0)) ## [1] 5
sum((A==1)&(B==1)&(C==1)) ## [1] 17
test<-data.frame(A=A,B=B,C=C)
count.reps(test)
# A B C Freq
# 1 0 0 1 26
# 2 0 1 0 9
# 3 1 0 1 10
# 4 1 0 0 7
# 5 1 1 1 15
# 6 0 0 0 11
#14 1 1 0 5
#25 0 1 1 17
So the freqencies come out in the order I found them ("binary
counting": ABC=000,001,010,011,100,101,110,111) but with the
(A,B,C) values in a quite different order.
I can't, myself, make out why this is happening, but no doubt
someone else can, and may sugest a correction!
Ted.
> Best,
> Erik Iverson
>
> ravi wrote:
>> Hi,
>> The unique function is easy to understand and use. Beyond that, I want
>> to get also the frequency of repetition of each individual row in a
>> data frame
>> Let me explain with an example :
>> x<-data.frame(a=c(1,2,3,1,2),b=c(2,3,4,2,3),c=c(10,20,30,10,20))
>> xu<-unique(x)
>> We have,
>>> x
>> a b c
>> 1 1 2 10
>> 2 2 3 20
>> 3 3 4 30
>> 4 1 2 10
>> 5 2 3 20
>>> xu
>> a b c
>> 1 1 2 10
>> 2 2 3 20
>> 3 3 4 30
>>
>> I want to get the following data frame :
>> a b c Freq
>> 1 1 2 10 2
>> 2 2 3 20 2
>> 3 3 4 30 1
>> That is, in addition to the unique rows, I want to get the frequency
>> of repetion of each individual row.
>> I will appreciate all the help that I can get.
>> Thank You,
>> Ravi
>>
>> ______________________________________________
>> 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.
>>
>
> ______________________________________________
> 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.
--------------------------------------------------------------------
E-Mail: (Ted Harding) <Ted.Harding at manchester.ac.uk>
Fax-to-email: +44 (0)870 094 0861
Date: 07-May-08 Time: 18:02:09
------------------------------ XFMail ------------------------------
More information about the R-help
mailing list