Thanks, that solved it!
Date: Mon, 30 Mar 2009 09:55:17 +0200
Subject: Re: [R] Sum of character vector
From: [email protected]
To: [email protected]
CC: [email protected]
Hi,
This: sum(data[,1]=="GG") may not work because you have some NA in your data.
Try this:
sum( data[ , 1 ] == "GG", na.rm = TRUE )
Regards
2009/3/30 David A.G <[email protected]>
Dear list,
I am trying to evaluate how many elements in a vector equal a certain value.
The vectors are the columns of a data.frame, read in using read.table():
> dim(data)
[1] 2600 ¨¢742
> data[1:5,1:5]
¨¢SNP001 SNP002 SNP003 SNP004 SNP005
1 ¨¢ ¨¢ GG ¨¢ ¨¢ AA ¨¢ ¨¢ TT ¨¢ ¨¢ TT ¨¢ ¨¢ GG
2 ¨¢ ¨¢ GG ¨¢ ¨¢ AA ¨¢ ¨¢ TC ¨¢ ¨¢ TT ¨¢ ¨¢ GG
3 ¨¢ ¨¢ GG ¨¢ ¨¢ AC ¨¢ ¨¢ CC ¨¢ ¨¢ TT ¨¢ ¨¢ GG
4 ¨¢ ¨¢ AG ¨¢ ¨¢ AA ¨¢ ¨¢ TT ¨¢ ¨¢ TT ¨¢ ¨¢ GG
5 ¨¢ ¨¢ GG ¨¢ ¨¢ AA ¨¢ ¨¢ TC ¨¢ ¨¢ TT ¨¢ ¨¢ GG
> table(data[,1])
¨¢AA ¨¢ AG ¨¢ GG
¨¢251 1093 1252
but if I do
> sum(data[,1]=="GG")
[1] NA
I have tried storing the column it in a vector but with same results:
> yyy<-(data[,1])
> sum(yyy=="GG")
[1] NA
while if I just get a small number of elements from this vector, it works fine
> yyyy <- yyy[1:10]
> yyyy
¨¢[1] "GG" "GG" "GG" "AG" "GG" "GG" "AA" "GG" "AG" "GG"
> table(yyyy)
yyyy
AA AG GG
¨¢1 ¨¢2 ¨¢7
> sum(yyyy=="GG")
[1] 7
I checked the archives for help but couldn©Èt find my error
What am I missing?
> sessionInfo()
R version 2.8.1 (2008-12-22)
i386-pc-mingw32
attached base packages:
[1] stats ¨¢ ¨¢ graphics ¨¢grDevices utils ¨¢ ¨¢ datasets ¨¢methods ¨¢ base
Thanks
Dave
_________________________________________________________________
¨¢ ¨¢ ¨¢ ¨¢[[alternative HTML version deleted]]
______________________________________________
[email protected] mailing list
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
_________________________________________________________________
[[elided Hotmail spam]]
[[alternative HTML version deleted]]
______________________________________________
[email protected] 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.