[R] Odp: Fw: Error in rowSums REPOST

Petr PIKAL petr.pikal at precheza.cz
Fri Aug 13 14:24:33 CEST 2010


Hi

r-help-bounces at r-project.org napsal dne 13.08.2010 13:50:20:

> For the query below I have also included the follwing information. 
Thanks for 
> your replies
> 
> > str(FeaturePresenceMatrix)
>  chr [1:65530, 1:40] "0" "0" "0" "0" "1" "0" "0" "0" "0" ...
>  - attr(*, "dimnames")=List of 2
>   ..$ : chr [1:65530] "4" "5" "6" "7" ...
>   ..$ : chr [1:40] "X1" "X2" "X3" "X4" ...
> > ?class
> > class(FeaturePresenceMatrix)
> [1] "matrix"
> 
> Amit Patel wrote:
> 
> > Hi 
> > I am trying to calculate the row sums of a matrix i have created
> > The matrix ( FeaturePresenceMatrix) has been created by
> > 
> > 1) Read csv

Result shall be data frame

> > 2) Removing unnecesarry data using [-1:4,] command

Rows 1:4 were probably non numeric so all data appeared non numeric.
and see difference

-1:4
-(1:4)

> > 3) replacing all the NA values with as.numeric(0) and all others with 
> >as.numeric (1)

Here you changed your data frame to matrix somehow which will than become 
non numeric. Beside rowSums behaves nicely wit NAs through na.rm=TRUE 
argument, so there is no need to change NA to zero.

Use is.na function instead if you want to count non NA elements

rowSums(!is.na(some.matrix))

Regards
Petr

> > 
> > When I carry out the command
> > 
> > TotalFeature <- rowrowSums(FeaturePresenceMatrix, na.rm = TRUE)
> > 
> > I get the following error. 
> > Error in rowSums(FeaturePresenceMatrix, na.rm = TRUE) :   'x' must be 
numeric
> > 
> > Any tips onhow I can get round this?
> 
> Yes, follow the posting guide and give the list a reproducible
> example. We don't know a critical piece of information,
> the class of your data. We know it's *not* numeric though,
> which is what it needs to be.  Use ?class, ?str, and
> possibly give us a small sample with ?dput. That way, we can
> reproduce the error.
> 
> 
> 
> 
> 
> ______________________________________________
> 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.



More information about the R-help mailing list