我写了一个 R 函数来返回满足特定要求的行,但是我遇到了输出格式的问题。
例如,返回具有特定值的行:
xx.1 <- data.frame(aa= 1:5, bb = c("AA","BB","CC","DD","EE"))
xx.2 <- c("AA","DD")
ff_in <- function(x,y){
if (toupper(x[2]) %in% y) {return(x)}
}
apply(xx.1,1,ff_in,xx.2)
但是,输出看起来像:
[[1]]
aa bb
"1" "AA"
[[2]]
NULL
[[3]]
NULL
[[4]]
aa bb
"4" "DD"
[[5]]
NULL
如何抑制 R 返回 null,并返回类似的东西?
aa bb
"1" "AA"
"4" "DD"