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 <saub...@yahoo.com>
To: Petr Savicky <savi...@cs.cas.cz>
Cc: "r-help@r-project.org" <r-help@r-project.org>
Sent: Thursday, 7 June 2012, 23:24
Subject: Re: [R] table function in a matrix
Perfect, thank you!
From: Petr Savicky <savi...@cs.cas.cz>
To: r-help@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@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@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@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.