[R] Looping through values in a data frame that are >zero
Berend Hasselman
bhh at xs4all.nl
Sat May 21 17:17:28 CEST 2011
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.
More information about the R-help
mailing list