我试图将整数类型列中存在的所有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] <- ''
}
答案 0 :(得分:2)
您可以使用across
中的dplyr
用class
替换列值:
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