Skip to content

select data

6 messages · R. Michael Weylandt, David L Carlson, David Winsemius +2 more

#
This was actually discussed about a week and a half ago with many good
solutions offered, but I think the most idiomatic would be something
like this:

apply(dataset, 1, function(x) mean(x[x>0]))

The reasons I like it:

i) It uses the apply function to do the same operation row-wise
(that's what the "1" does) to all elements of your data set -- since
this is side-effect free (as a good functional language should be) it
makes for easy parallelization if you move to "big data"
ii) It uses an anonymous function (the "function ... " bit) which are
first class objects in R and can be passed as arguments to other
functions (here apply())
iii) It uses logical subscripting to pick out the values greater than
zero -- I think the subscripting behavior is the very best bit of R

Best,

Michael
On Mon, May 14, 2012 at 12:32 PM, Andrea Sica <aerdna.sica at gmail.com> wrote:
#
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)

----------------------------------------------
David L Carlson
Associate Professor of Anthropology
Texas A&M University
College Station, TX 77843-4352
#
On May 14, 2012, at 12:32 PM, Andrea Sica wrote:

            
perhaps (untested in absence of reproducble example):

apply(dfrm, 1, function(x) mean(x[x>=0]) ) # would also work for a  
matrix object.
David Winsemius, MD
West Hartford, CT
#
________________________________________
'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}}