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@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.

Reply via email to