[R] table function in a matrix
Rui Barradas
ruipbarradas at sapo.pt
Mon Jul 2 10:09:15 CEST 2012
Hello,
See the difference.
a <- b <- c("A", "A", "B", "B", "C", "A", "C", "D", "A", "D", "C", "A",
"D", "C", "A", "C")
a[3] <- NA
table(a)
table(a, exclude=NULL) # always include NA
table(b, exclude=NULL) # always include NA
# more flexible
table(b, useNA="always")
table(b, useNA="ifany")
Hope this helps,
Rui Barradas
Em 02-07-2012 07:27, Sarah Auburn escreveu:
> Dear Petr,
> Thanks for your help. Sorry one more query for one of my datasets which has NAs (missing genotypes). Is there any way in which I can count NAs?
> Many thanks!
> Sarah
>
> From: Sarah Auburn <sauburn at yahoo.com>
> To: Petr Savicky <savicky at cs.cas.cz>
> Cc: "r-help at r-project.org" <r-help at r-project.org>
> Sent: Thursday, 7 June 2012, 23:24
> Subject: Re: [R] table function in a matrix
>
>
> Perfect, thank you!
>
> From: Petr Savicky <savicky at cs.cas.cz>
> To: r-help at r-project.org
> Sent: Thursday, 7 June 2012, 19:42
> Subject: Re: [R] table function in a matrix
>
> On Wed, Jun 06, 2012 at 11:02:46PM -0700, Sarah Auburn wrote:
>> Hi,
>> I am trying to get a summary of the counts of different variables for each sample in a matrix of the form "m" below to generate an output as shown. (Ultimately I want to generate a stacked barchart for each sample). I am only able to get the "table" function to work on one sample (column) at a time. Any help appreciated.
>> Thank you
>> Sarah
>> ?
>> a<-c("A", "A", "B", "B", "C", "A", "C", "D", "A", "D", "C", "A", "D", "C", "A", "C")
>> m<-matrix(a, nrow=4)
>> m
>> ???? [,1] [,2] [,3] [,4]
>> [1,] "A"? "C"? "A"? "D"
>> [2,] "A"? "A"? "D"? "C"
>> [3,] "B"? "C"? "C"? "A"
>> [4,] "B"? "D"? "A"? "C"
>>
>> output needed (so that I can use the "barplot(t(output))" function):
>> A B C D
>> [,1] 2 2 0 0
>> [,2] 1 0 2 1
>> [,3] 2 0 1 1
>> [,4] 1 0 2 1
>
> Hi.
>
> Try the following.
>
> a<-c("A", "A", "B", "B", "C", "A", "C", "D", "A", "D", "C", "A", "D", "C", "A", "C")
> m<-matrix(a, nrow=4)
> tab <- function(x) { table(factor(x, levels=LETTERS[1:4])) }
> t(apply(m, 2, tab))
>
> A B C D
> [1,] 2 2 0 0
> [2,] 1 0 2 1
> [3,] 2 0 1 1
> [4,] 1 0 2 1
>
> Factors are used to ensure that all the tables have the same length,
> even if some letters are missing.
>
> Hope this helps.
>
> Petr Savicky.
>
> ______________________________________________
> 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.
> [[alternative HTML version deleted]]
>
>
>
> ______________________________________________
> 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