我想创建一个函数,用数据框的因子变量中的文本“ NR”替换NA。
我在网上找到了下面的代码,效果很好:
i <- sapply(data_5, is.factor) # Identify all factor variables in your data
data_5[i] <- lapply(data_5[i], as.character) # Convert factors to character variables
data_5[is.na(data_5)] <- 0 # Replace NA with 0
data_5[i] <- lapply(data_5[i], as.factor) # Convert character columns back to factors
但是我想在一个名为“ remove_na_factor”的函数中转换此代码。我尝试如下:
remove_na_factor <- function(x){
i <- sapply(x, is.factor) # Identify all factor variables in your data
x[i] <- lapply(x[i], as.character) # Convert factors to character variables
x[is.na(x)] <- "NR" # Replace NA with NR
x[i] <- lapply(x[i], as.factor) # Convert character columns back to factors
}
当我在具有NA值的数据帧上运行该函数时,没有任何反应... 预先感谢您的帮助。
答案 0 :(得分:1)
只需在函数末尾添加return(x)
:
remove_na_factor <- function(x){
#your function body
return(x)
}
您还可以使用tidyverse
方法获得相同的结果
library(tidyverse)
x %>%
mutate_if(is.factor, as.character) %>% # Convert factors to character variables
mutate_if(is.character, replace_na, "NR") %>% # Replace NA with NR
mutate_if(is.character, as.factor) # Convert character columns back to factors