# Another bit of R

/R makes it easy to chop up and reassemble data frames, whether it's with the subset() function or with dplyr's filter() function. It isn't always obvious how to do it for a whole data frame. That's where apply() is useful.

This will filter a data frame such that all columns have at least one value less than zero in any row, for example:

newdata.df <- data.df[ ,apply(data.df, MARGIN = 2, function(x) any(x < 1))]

any() can be exhanged for all() to restrict the selection to columns in which all values are less than one.

The same code works for rows: just move the apply(...) in front of the comma, then change MARGIN to 1 instead of 2.

This will filter a data frame such that all columns have at least one value less than zero in any row, for example:

newdata.df <- data.df[ ,apply(data.df, MARGIN = 2, function(x) any(x < 1))]

any() can be exhanged for all() to restrict the selection to columns in which all values are less than one.

The same code works for rows: just move the apply(...) in front of the comma, then change MARGIN to 1 instead of 2.