[R] select data

S Ellison S.Ellison at LGCGroup.com
Mon May 14 23:26:16 CEST 2012


________________________________________
>  From: David L Carlson [dcarlson at 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}}



More information about the R-help mailing list