2
v1 = c(1,2,3)
v2 = c("a","b",NA)
X = data.frame(v1,v2)

f = function(X,d){
    subset(X,is.na(d)==0)
    }
f(X,"v2")

How can I get the subset of X for which any given column (inputted into the argument of a function) isn't missing?

generic_user
  • 3,430
  • 3
  • 32
  • 56

3 Answers3

5

Note: The function subset should not be used in functions but interactively only (see here).

f <- function(X, d) {
  X[!is.na(X[d]), ]
}

> f(X,"v2")
  v1 v2
1  1  a
2  2  b
Community
  • 1
  • 1
Sven Hohenstein
  • 80,497
  • 17
  • 145
  • 168
3

If you use complete.cases you can input a vector of column names.

f <- function(X,d) {
     X[complete.cases(X[,d]),]
 }
Geoffrey Absalom
  • 1,815
  • 15
  • 16
1

You don't need a function. Just do:

X[!is.na(X$v2),]
Thomas
  • 43,637
  • 12
  • 109
  • 140
  • True, but my interpretation of the OP's question was how to write this sort of function in the general case, where rewriting the arguments to the `[` operator might not be so simple. – Carl Witthoft Jun 12 '13 at 11:52
  • Yes, after I posted I wondered if the question was about genuinely needing a function or just not knowing how to subset without `subset`. Others have since provided answers to the former, however. – Thomas Jun 12 '13 at 13:13