[R] How to globally convert NaN to NA in dataframe?

Duncan Murdoch murdoch@dunc@n @end|ng |rom gm@||@com
Thu Sep 2 21:44:07 CEST 2021


On 02/09/2021 3:20 p.m., Greg Minshall wrote:
> Andrew,
> 
>> x[] <- lapply(x, function(xx) {
>>      xx[is.nan(xx)] <- NA_real_
>>      xx
>> })
>>
>> is different from
>>
>> x <- lapply(x, function(xx) {
>>      xx[is.nan(xx)] <- NA_real_
>>      xx
>> })
> 
> indeed, the two are different -- but some ignorance of mine is exposed.
> i wonder, can you explain why the two are different?

x <- lapply(...) says "set x to the list on the RHS", so x becomes a 
list, not a dataframe.

x[] <- lapply(...) says "set the values in x to the values in the list 
on the RHS", so x retains its class.

Duncan Murdoch



More information about the R-help mailing list