replacing NaN for every attribute in my data

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

replacing NaN for every attribute in my data

mishkind
Hi,
I have a list called ds which has the following attributes:

attributes(ds)
$names
[1] "adj.r.squared" "fstatistic"    "intercept"     "slope"        
[5] "std.error"     "tstatistic"  

I want to replace all the NaN is ds with 0, and after searching past posts I found I can hardcode it like this:
ds$adj.r.squared=replace(ds$adj.r.squared,is.nan(ds$adj.r.squared),0)
ds$fstatistic=replace(ds$fstatistic,is.nan(ds$fstatistic),0)
...
ds$tstatistic=replace(ds$tstatistic,is.nan(ds$tstatistic),0)

But, there has to be a more elegant/general solution. I've tried messing around with lapply but can't seem to get it to work.

Any suggestion?
thanks,
mishkin
Reply | Threaded
Open this post in threaded view
|

Re: replacing NaN for every attribute in my data

Yasir Kaheil
You can try this:
 for (i in 1:length(ds)) {
dummy<-ds[[i]];
dummy[is.nan(dummy)]<-0
ds[[i]]<-dummy
}


if is.nan doesn't work, replace with is.na
Yasir Kaheil
Reply | Threaded
Open this post in threaded view
|

Re: replacing NaN for every attribute in my data

mishkind
Thanks. That worked.