简体   繁体   English

用因子变量替换数据框中的NA

[英]Replace NA in a data frame with factor variables

I would like to create a function to replace NA by the text "NR" in factor variables of a data frame. 我想创建一个函数,用数据框的因子变量中的文本“ NR”替换NA。

I found the below code on the web, that works perfectly : 我在网上找到了下面的代码,效果很好:

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

But I would like to transform this code in a function called "remove_na_factor". 但是我想在一个名为“ remove_na_factor”的函数中转换此代码。 I tried as below : 我尝试如下:

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

} }

When when I run the function on a data frame with NA values, nothing happens ... Thanks in advance for your help. 当我在具有NA值的数据帧上运行该函数时,什么也没有发生……预先感谢您的帮助。

Just add return(x) at the end of your function: 只需在函数末尾添加return(x)

remove_na_factor <- function(x){
  #your function body
  return(x)
}

You can also get the same result using a tidyverse approach 您还可以使用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  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM