如果列是数字,则用0填充NA,如果列是使用R的因数,则用空字符串''填充

时间:2020-07-09 06:34:17

标签: r dataframe na

我试图将整数类型列中存在的所有NA替换为0,并将因子类型列中存在的NA替换为空字符串“”。下面的代码是我正在使用的代码,但似乎不起作用

for(i in 1:ncol(credits)){
if(sapply(credits[i], class) == 'integer'){
    credits[is.na(credits[,i]), i] <- 0
}
else if(sapply(credits[i], class) == 'factor'){
    credits[is.na(credits[,i]), i] <- ''
}

2 个答案:

答案 0 :(得分:2)

您可以使用across中的dplyrclass替换列值:

library(dplyr)

df %>%
  mutate(across(where(is.factor), ~replace(as.character(.), is.na(.), '')),
         across(where(is.numeric), ~replace(., is.na(.), 0)))


#  a b
#1 1 a
#2 2 b
#3 0 c
#4 4 d
#5 5  

b列现在属于“字符”类,如果您需要将其作为factor,则可以在factor之外添加replace,如:

across(where(is.factor), ~factor(replace(as.character(.), is.na(.), ''))),

数据

df <- data.frame(a = c(1, 2, NA, 4:5), b = c(letters[1:4], NA), 
                 stringsAsFactors = TRUE)

答案 1 :(得分:0)

另一种实现此目的的方法:

library(dplyr)

# Dataframe
df <- data.frame(x = c(1, 2, NA, 4:5), y = c('a',NA, 'd','e','f'), 
             stringsAsFactors = TRUE)

# Creating new columns
df_final<- df %>% 
  mutate(new_x = ifelse(is.numeric(x)==TRUE & is.na(x)==TRUE,0,x)) %>% 
  mutate(new_y = ifelse(is.factor(y)==TRUE & is.na(y)==TRUE,"",y))

# Printing the output
df_final