
Thanks, that solved it!




    Date: Mon, 30 Mar 2009 09:55:17 +0200
Subject: Re: [R] Sum of character vector
From: r.user.spain@gmail.com
To: dasolexa@hotmail.com
CC: r-help@r-project.org

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 <dasolexa@hotmail.com>



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]]




______________________________________________

R-help@r-project.org 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]]


