________________________________________ > From: David L Carlson [dcarl...@tamu.edu] >This overwrites the data so you might want to create a copy first. > > example <- data.frame(V1=c(3, -1), V2=c(-2, 4), V3=c(4, 1)) > tf <- ifelse(example<0, TRUE, FALSE) > example[tf] <- NA > apply(example, 1, mean, na.rm=TRUE)
'simpler' to do something like apply(example, 1, function(x) mean(x[x>=0])) Also note that this is averaging _rows_; if you want the column (variable) means, which would be much more usual in a data frame, use apply(example, 2, function(x) mean(x[x>=0])) S Ellisn******************************************************************* This email and any attachments are confidential. Any use...{{dropped:8}} ______________________________________________ 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.