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?
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?
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
If you use complete.cases you can input a vector of column names.
f <- function(X,d) {
X[complete.cases(X[,d]),]
}
You don't need a function. Just do:
X[!is.na(X$v2),]