[R] Counting numbers in R
S Ellison
S.Ellison at lgcgroup.com
Fri Oct 4 15:03:35 CEST 2013
> I have a set of data and I need to find out how many points are below a
> certain value but R will not calculate this properly for me.
R will. But you aren't.
> Negative numbers seem to be causing the issue.
You haven't got any negative numbers in your data set. In fact, you haven't got any numbers. It's all character strings. Is there a reason for that?
Assuming there is, if you have your data in a data frame 'A' and just want the count:
table(as.numeric(A$Tm_ugL) <= 0.0002)
If you just want a complete vector of TRUE or FALSE
as.numeric(d$Tm_ugL) <= 0.0002)
does that. If you want to add that to your data frame (is it called A?) that looks like
A$Censored <- as.numeric(d$Tm_ugL) <= 0.0002)
But you really shouldn't have numbers in character format; read it as numeric. Then it's just
table(d$Tm_ugL <= 0.0002) and so on. If it's refusing to read as numeric, find out why and fix the data.
And some comments on code, while I'm here:
> for (i in one:nrow(A))
...
> if (A[i,two]<=A_LLD)
Variables called 'one' and 'two' look like a really bad idea. If they are equal to 1 and 2, use 1 and 2 (or 1L and 2L if you want to be _sure_ they are integer). If not, the names are going to be pretty confusing, no?
> (A_Censored[i,two]<-"TRUE")
Why use a character string like "TRUE" that R can't interpret as logical instead of the logical values TRUE and FALSE?
S Ellison
*******************************************************************
This email and any attachments are confidential. Any use...{{dropped:8}}
More information about the R-help
mailing list