我寫了一個R函數來返回滿足特定需求的行,但是我遇到了輸出格式的問題。如何禁止函數返回R中的「Null」?
例如,某些值返回行:
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"
它令人驚歎!謝謝! – shao 2012-04-18 07:16:02