Skip to content
Back to formatted view

Raw Message

Message-ID: <9df5d40e-f922-1282-025f-2b6b0b09e2e6@gmail.com>
Date: 2021-09-02T19:44:07Z
From: Duncan Murdoch
Subject: How to globally convert NaN to NA in dataframe?
In-Reply-To: <1021538.1630610456@apollo2.minshall.org>

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