R-将nan转换为0的结果全为0 [英] R - convert nan to 0 results in all 0's

查看:226
本文介绍了R-将nan转换为0的结果全为0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据帧,其中包含要转换为0的NaN.我写了一个我认为应该起作用的函数:

I have a data frame containing NaN's that I'd like to convert to 0's. I wrote a function that I think should work:

fix_nan <- function(x){
    return(x[is.nan(x)] <- 0)
}

然后将其应用于数据框:

And then I apply it to the data frame:

train_e <- structure(list(pack_id = structure(1:10, .Label = c("1", "2", 
"4", "5", "7", "8", "9", "10", "11", "14"), class = "factor"), 
    item_1 = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0), item_2 = c(NaN, 
    NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN), item_3 = c(1.45225232891169, 
    0.613104472886409, NaN, 1.02450431651439, 0.735706794978741, 
    0.741937344729377, NaN, 0.83034830207343, 0.97650959186721, 
    0.750305594399894), item_4 = c(0.645137961373585, 0.615792803650477, 
    Inf, 0.752866415261568, 0.84901755126673, 0.646398200985872, 
    Inf, 0.786548355648346, 0.725113372622438, 0.709897990984761
    ), item_5 = c(NaN, NaN, NaN, 0, 0, 0, NaN, NaN, 0, 0), item_6 = c(0.510825623765991, 
    0.510825623765991, NaN, 0.510825623765991, 0.510825623765991, 
    0.510825623765991, NaN, 0.510825623765991, 0.847297860387204, 
    0.510825623765991)), .Names = c("pack_id", "item_1", "item_2", 
"item_3", "item_4", "item_5", "item_6"), row.names = c(26155L, 
6236L, 6281L, 6014L, 6035L, 26217L, 5576L, 6316L, 5594L, 26244L
), class = "data.frame")
vtf1 <- c('item_1','item_2','item_3','item_4','item_5','item_6')


train_e[,vtf1] <- as.data.frame(lapply(train_e[,vtf1], fix_nan))
head(train_e)

我得到全0:

> head(train_e)
      pack_id item_1 item_2 item_3 item_4 item_5 item_6
26155       1      0      0      0      0      0      0
6236        2      0      0      0      0      0      0
6281        4      0      0      0      0      0      0
6014        5      0      0      0      0      0      0
6035        7      0      0      0      0      0      0
26217       8      0      0      0      0      0      0

有什么建议吗?

推荐答案

x[is.nan(x)] <- 0仅返回x中的NaN元素(现在为零).要解决此问题,请更改功能:

x[is.nan(x)] <- 0 returns only those elements of x that were NaN (and are now zero). To fix this, change your function:

fix_nan <- function(x){
    x[is.nan(x)] <- 0
    x
}

这篇关于R-将nan转换为0的结果全为0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆