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

From: Berend Hasselman <>
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


or this



View this message in context:
Sent from the R help mailing list archive at

______________________________________________ mailing list
PLEASE do read the posting guide
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 Please read the posting guide before posting to the list.

list of date sections of archive