[R] Calculating frequencies of multiple values in 200 colomns

Boris Steipe boris.steipe at utoronto.ca
Fri Nov 10 14:43:25 CET 2017


|> x <- sample(0:2, 10, replace = TRUE)
|> x
 [1] 1 0 2 1 0 2 2 0 2 1
|> tabulate(x)
[1] 3 4
|> table(x)
x
0 1 2 
3 3 4 



B.



> On Nov 10, 2017, at 4:32 AM, Allaisone 1 <allaisone1 at hotmail.com> wrote:
> 
> 
> 
> Thank you for your effort Bert..,
> 
> 
> I knew what is the problem now, the values (1,2,3) were only an example. The values I have are 0 , 1, 2 . Tabulate () function seem to ignore calculating the frequency of 0 values and this is my exact problem as the frequency of 0 values should also be calculated for the maf to be calculated correctly.
> 
> ________________________________
> From: Bert Gunter <bgunter.4567 at gmail.com>
> Sent: 09 November 2017 23:51:35
> To: Allaisone 1; R-help
> Subject: Re: [R] Calculating frequencies of multiple values in 200 colomns
> 
> [[elided Hotmail spam]]
> 
> "For example, if I have the values : 1 , 2 , 3 in each column, applying Tabulate () would calculate the frequency of 1 and 2 without 3"
> 
> Huh??
> 
>> x <- sample(1:3,10,TRUE)
>> x
> [1] 1 3 1 1 1 3 2 3 2 1
>> tabulate(x)
> [1] 5 2 3
> 
> Cheers,
> Bert
> 
> 
> 
> Bert Gunter
> 
> "The trouble with having an open mind is that people keep coming along and sticking things into it."
> -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )
> 
> On Thu, Nov 9, 2017 at 3:44 PM, Allaisone 1 <allaisone1 at hotmail.com<mailto:allaisone1 at hotmail.com>> wrote:
> 
> Thank you so much for your replay
> 
> 
> Actually, I tried apply() function but struggled with the part of writing the appropriate function inside it which calculate the frequency of the 3 values. Tabulate () function is a good start but the problem is that this calculates the frequency of two values only per column which means that when I apply maf () function , maf value will be calculated using the frequency of these 2 values only without considering the frequency of the 3rd value. For example, if I have the values : 1 , 2 , 3 in each column, applying Tabulate () would calculate the frequency of 1 and 2 without 3 . I need a way to calculate the frequencies of all of the 3 values so the calculation of maf will be correct as it will consider all the 3 frequencies but not only 2 .
> 
> 
> Regards
> 
> Allahisone
> 
> ________________________________
> From: Bert Gunter <bgunter.4567 at gmail.com<mailto:bgunter.4567 at gmail.com>>
> Sent: 09 November 2017 20:56:39
> To: Allaisone 1
> Cc: r-help at R-project.org
> Subject: Re: [R] Calculating frequencies of multiple values in 200 colomns
> 
> This is not a good way to do things! R has many powerful built in functions to do this sort of thing for you. Searching  -- e.g. at rseek.org<http://rseek.org> or even a plain old google search -- can help you find them. Also, it looks like you need to go through a tutorial or two to learn more about R's basic functionality.
> 
> In this case, something like (no reproducible example given, so can't confirm):
> 
> apply(Values, 2, function(x)maf(tabulate(x)))
> 
> should be close to what you want .
> 
> 
> Cheers,
> Bert
> 
> 
> 
> 
> 
> 
> 
> Bert Gunter
> 
> "The trouble with having an open mind is that people keep coming along and sticking things into it."
> -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )
> 
> On Thu, Nov 9, 2017 at 11:44 AM, Allaisone 1 <allaisone1 at hotmail.com<mailto:allaisone1 at hotmail.com>> wrote:
> 
> Hi All
> 
> 
> I have a dataset of 200 columns and 1000 rows , there are 3 repeated values under each column (7,8,10). I wanted to calculate the frequency of each value under each column and then apply the function maf () given that the frequency of each value is known. I can do the analysis step by step like this :-
> 
> 
>> Values
> 
> 
>         A       B       C       ... 200
> 
> 1      7       10      7
> 
> 2      7       8        7
> 
> 3      10     8        7
> 
> 4       8      7         10
> 
> .
> 
> .
> 
> .
> 
> 
> 
> 
> For column A : I calculate the frequency for the 3 values as follows :
> 
> count7 <- length(which(Values$A == 7))
> 
> count8 <- length(which(Values$A == 8))
> 
> count10 <- length(which(Values$A == 10))
> 
> 
> count7 = 2, count8 = 1 , count10= 1.
> 
> 
> Then, I create a vector  and type the frequencies manually :
> 
> 
> Freq<- c( count7=2  ,count8= 1,count10=1)
> 
> 
> Then I apply the function maf ()  :-
> 
> maf(Freq)
> 
> 
> This gives me the result I need for column A , could you please help me
> 
> to perform the analysis for all of the 200 columns at once ?
> 
> 
> Regards
> 
> Allahisone
> 
> 
>        [[alternative HTML version deleted]]
> 
> ______________________________________________
> R-help at r-project.org<mailto:R-help at r-project.org> mailing list -- To UNSUBSCRIBE and more, see
> 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.
> 
> 
> 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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