Hi
I am trying to create a column in a data frame which gives a sigificane score
from 0-7. It should read values from 7 different colums and add 1 to the counter
if the value is <=0.05. I get an error message saying
Error in if (ALLRESULTS[i, 16] <= 0.05) significance_count =
significance_count + :
missing value where TRUE/FALSE needed
The script is included below
it works if i convert the NA values to zero but this is not appropriate as it
includes the zero as significant.
ANY SUGGESTIONS
#SCRIPT STARTS
for (i in 1:length(ALLRESULTS[,1])) {
significance_count = 0
if (ALLRESULTS[i,16] <= 0.05 ) significance_count = significance_count +1
else significance_count = significance_count
if (ALLRESULTS[i,17] <= 0.05 ) significance_count = significance_count +1
else significance_count = significance_count
if (ALLRESULTS[i,18] <= 0.05 ) significance_count = significance_count +1
else significance_count = significance_count
if (ALLRESULTS[i,19] <= 0.05 ) significance_count = significance_count +1
else significance_count = significance_count
if (ALLRESULTS[i,20] <= 0.05 ) significance_count = significance_count +1
else significance_count = significance_count
if (ALLRESULTS[i,21] <= 0.05 ) significance_count = significance_count +1
else significance_count = significance_count
if (ALLRESULTS[i,22] <= 0.05 ) significance_count = significance_count +1
else significance_count = significance_count
ALLRESULTS[i,23] <- significance_count}
Dear Amit, Try this: significance_count <- apply( ALLRESULTS[,16:22], 1, function(x) sum( x <0.05 ) ) ) HTH, Jorge On Tue, Jun 9, 2009 at 12:17 PM, Amit Patel <amitrhelp@yahoo.co.uk> wrote:> > Hi > I am trying to create a column in a data frame which gives a sigificane > score from 0-7. It should read values from 7 different colums and add 1 to > the counter if the value is <=0.05. I get an error message saying > > Error in if (ALLRESULTS[i, 16] <= 0.05) significance_count > significance_count + : > missing value where TRUE/FALSE needed > > The script is included below > > it works if i convert the NA values to zero but this is not appropriate as > it includes the zero as significant. > > ANY SUGGESTIONS > > > > #SCRIPT STARTS > for (i in 1:length(ALLRESULTS[,1])) { > significance_count = 0 > > if (ALLRESULTS[i,16] <= 0.05 ) significance_count = significance_count +1 > else significance_count = significance_count > if (ALLRESULTS[i,17] <= 0.05 ) significance_count = significance_count +1 > else significance_count = significance_count > if (ALLRESULTS[i,18] <= 0.05 ) significance_count = significance_count +1 > else significance_count = significance_count > if (ALLRESULTS[i,19] <= 0.05 ) significance_count = significance_count +1 > else significance_count = significance_count > if (ALLRESULTS[i,20] <= 0.05 ) significance_count = significance_count +1 > else significance_count = significance_count > if (ALLRESULTS[i,21] <= 0.05 ) significance_count = significance_count +1 > else significance_count = significance_count > if (ALLRESULTS[i,22] <= 0.05 ) significance_count = significance_count +1 > else significance_count = significance_count > > ALLRESULTS[i,23] <- significance_count} > > > > > ______________________________________________ > 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]]
Try this:
for (i in 1:dim(ALLRESULTS)[1]) {
ALLRESULTS[i,23] <- length(ALLRESULTS[i,][ALLRESULTS[i,16:22] <= 0.05])
}
On Wed, Jun 10, 2009 at 12:17 AM, Amit Patel<amitrhelp at yahoo.co.uk>
wrote:>
> Hi
> I am trying to create a column in a data frame which gives a sigificane
score from 0-7. It should read values from 7 different colums and add 1 to the
counter if the value is <=0.05. I get an error message saying
>
> Error in if (ALLRESULTS[i, 16] <= 0.05) significance_count =
significance_count + ?:
> ?missing value where TRUE/FALSE needed
>
> The script is included below
>
> it works if i convert the NA values to zero but this is not appropriate as
it includes the zero as significant.
>
> ANY SUGGESTIONS
>
>
>
> #SCRIPT STARTS
> for (i in 1:length(ALLRESULTS[,1])) {
> significance_count = 0
>
> if (ALLRESULTS[i,16] <= 0.05 ) ?significance_count = significance_count
+1 else significance_count = significance_count
> if (ALLRESULTS[i,17] <= 0.05 ) ?significance_count = significance_count
+1 else significance_count = significance_count
> if (ALLRESULTS[i,18] <= 0.05 ) ?significance_count = significance_count
+1 else significance_count = significance_count
> if (ALLRESULTS[i,19] <= 0.05 ) ?significance_count = significance_count
+1 else significance_count = significance_count
> if (ALLRESULTS[i,20] <= 0.05 ) ?significance_count = significance_count
+1 else significance_count = significance_count
> if (ALLRESULTS[i,21] <= 0.05 ) ?significance_count = significance_count
+1 else significance_count = significance_count
> if (ALLRESULTS[i,22] <= 0.05 ) ?significance_count = significance_count
+1 else significance_count = significance_count
>
> ALLRESULTS[i,23] <- significance_count}
>
>
>
>
> ______________________________________________
> 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.
>
On 6/9/2009 12:17 PM, Amit Patel wrote:> Hi > I am trying to create a column in a data frame which gives a sigificane score from 0-7. It should read values from 7 different colums and add 1 to the counter if the value is <=0.05. I get an error message saying > > Error in if (ALLRESULTS[i, 16] <= 0.05) significance_count = significance_count + : > missing value where TRUE/FALSE needed > > The script is included below > > it works if i convert the NA values to zero but this is not appropriate as it includes the zero as significant. > > ANY SUGGESTIONSsignificance.count <- rowSums(ALLRESULTS[,16:22] <= .05, na.rm=TRUE) ?rowSums> #SCRIPT STARTS > for (i in 1:length(ALLRESULTS[,1])) { > significance_count = 0 > > if (ALLRESULTS[i,16] <= 0.05 ) significance_count = significance_count +1 else significance_count = significance_count > if (ALLRESULTS[i,17] <= 0.05 ) significance_count = significance_count +1 else significance_count = significance_count > if (ALLRESULTS[i,18] <= 0.05 ) significance_count = significance_count +1 else significance_count = significance_count > if (ALLRESULTS[i,19] <= 0.05 ) significance_count = significance_count +1 else significance_count = significance_count > if (ALLRESULTS[i,20] <= 0.05 ) significance_count = significance_count +1 else significance_count = significance_count > if (ALLRESULTS[i,21] <= 0.05 ) significance_count = significance_count +1 else significance_count = significance_count > if (ALLRESULTS[i,22] <= 0.05 ) significance_count = significance_count +1 else significance_count = significance_count > > ALLRESULTS[i,23] <- significance_count} > > > > > ______________________________________________ > 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.-- Chuck Cleland, Ph.D. NDRI, Inc. (www.ndri.org) 71 West 23rd Street, 8th floor New York, NY 10010 tel: (212) 845-4495 (Tu, Th) tel: (732) 512-0171 (M, W, F) fax: (917) 438-0894
<quote> > Error in if (ALLRESULTS[i, 16] <= 0.05) significance_count = significance_count + : > missing value where TRUE/FALSE needed > > The script is included below > > it works if i convert the NA values to zero but this is not appropriate as it includes the zero as significant. > > ANY SUGGESTIONS significance.count <- rowSums(ALLRESULTS[,16:22] <= .05, na.rm=TRUE) ?rowSums </quote> Good solution. I would like to comment on the OP's naivete: since he knows that zero is a value of interest to his algorithm, why the heck replace "NA" with zero? In general, if there is some reason not to use na.rm or na.omit, start out by thinking about what NA values mean to your analysis. If they are the equivalent of "FALSE" conditions, then replace them with a numerical value which is "FALSE". In this example, replacing NA with 0.051 would do just fine.