Re: [R] Looping through values in a data frame that are >zero

From: Berend Hasselman <bhh_at_xs4all.nl>
Date: Sat, 21 May 2011 08:17:28 -0700 (PDT)

Dimitri Liakhovitski-2 wrote:
>
> Hello!
>
> I've tried for a while - but can't figure it out. I have data frame x:
>
> y=c("a","b","c","d","e")
> z=c("m","n","o","p","r")
> a=c(0,0,1,0,0)
> b=c(2,0,0,0,0)
> c=c(0,0,0,4,0)
> x<-data.frame(y,z,a,b,c,stringsAsFactors=F)
> str(x)
> Some of the values in columns a,b, and c are >0:
>
> I need to write a loop through all the cells in columns a,b,c that are

>>0 (only through them).

> For each of those cells, I need to know:
> 1. Name of the column it is in
> 2 The entry of column y that is in the same row
> 3 The entry of column z that is in the same row
> It'd be good to save this info in a data frame somehow - so that I
> could loop through rows of this data frame.
>

This will give you a dataframe

x[-which(rowSums(x[,3:5]>0)==0),]

or this

x[-which(rowSums(x[,c("a","b","c")]>0)==0),]

Berend

--
View this message in context: http://r.789695.n4.nabble.com/Looping-through-values-in-a-data-frame-that-are-zero-tp3540579p3540752.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.
Received on Sat 21 May 2011 - 15:21:45 GMT

This quarter's messages: by month, or sorted: [ by date ] [ by thread ] [ by subject ] [ by author ]

All messages

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Sat 21 May 2011 - 15:50:08 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive