[R] Odp: Extract rows with non-zero elements

Petr PIKAL petr.pikal at precheza.cz
Tue May 4 11:21:32 CEST 2010


Hi

r-help-bounces at r-project.org napsal dne 04.05.2010 09:50:28:

> 
> Dear all,
> 
> In my dataset I have 12 columns and 5824 rows. The second column 
contains
> information about the height of a claim: it might be zero or positive. I
> would like to do an analysis on the positive part of this matrix, but I 
do
> need the other colums with this. So if there are like 1000 rows where 
the
> second column is positive, I would like to extract this 1000x13 matrix 
into
> another matrix.
> 
> The beginning of my code is:
> 
> Cars <- read.csv(filename, header=TRUE, row.names=NULL, sep=";")
> attach(Cars)

Do not do that. you will have troubles with referencing objects and 
changing them, unless you perfectly know which object you 
modify/reference.

> n <- as.numeric(n)
> s <- as.numeric(s)

you either have n and s as numeric than there is no need for as.numeric or 
they come aout as factors and in that case you get numerical 
***representation*** of levels, which is probably not what you want.

> # et cetera, the other variabeles are declared as numeric or as factors
> 
> # I've tried to extract the data the following way:
> Cars2 <- Cars[Cars$s!=0]

see ?"[" and matrix/data.frame indexing

Cars2 <- Cars[Cars$s!=0, ]

> 
> #However, then I get the error message:
> Error in `[.data.frame`(Cars, Cars$s != 0) : undefined columns selected
> 
> # If I try 
> s[s!=0]
> # I do get the positive s-values, but how can I get all colums in a 
matrix?

Cars is ***not*** matrix but data.frame. Seems to be minor difference but 
it is like mountain bike and racing bike. They look similar but their use 
and properties are quite different.

Regards
Petr


> 
> Does anyone know how this can be done? Your help would be greatly
> appreciated.
> 
> Regards,
> 
> Frank van Berkum
> 
> 
> 
> -- 
> View this message in context: 
http://r.789695.n4.nabble.com/Extract-rows-with-
> non-zero-elements-tp2125128p2125128.html
> Sent from the R help mailing list archive at Nabble.com.
> 
> ______________________________________________
> 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