简体   繁体   English

在多个数据帧列中重新编码NA

[英]Recode NAs in multiple dataframe columns

I have multiple integer columns in a data frame, all with NAs that I need to recode to 0. 我在数据框中有多个整数列,所有这些都是我需要重新编码为0的NA。

df1 <- as.data.frame(sapply(paste(sample(letters,50,T),sample(letters,10), sep=""), function(x) {sample(c(NA,0:5),10,T)} ))
df2 <- as.data.frame(sapply(paste(sample(letters,5,T),sample(letters,10,T), sep=""), function(x) {sample(letters[1:5],10,T)} ))
df <- cbind(df2,df1)

Producing an output like this... (only the first few columns of the 55 shown) 生成这样的输出...(仅显示55的前几列)

在此输入图像描述

I can go about recoding the NAs to 0 manually like df$col[is.na(df$col)] <- 0 for each column, but given that there are so many columns, it would take a while to type that all out. 我可以手动将df$col[is.na(df$col)] <- 0重新编码为0,如df$col[is.na(df$col)] <- 0 ,但鉴于列数太多,需要一段时间才能输入所有列。

How can I recode all of these NAs to 0 in a line or three? 如何在一行或三行中将所有这些NA重新编码为0?

(I realise I could melt the integer columns and then recode the one melted column, but I'd rather do this in base R) (我意识到我可以融化整数列,然后重新编码一个熔化的列,但我宁愿在基础R中这样做)

你非常接近:

df[is.na(df)] <- 0

使用plyrcolwise元函数可以轻松实现:

dfZ=colwise(function(x)ifelse(is.na(x),0,x))(df)

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

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