It seems like you might've missed one more thing, you need the brackets
next to 'x' to get it to work.
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
})
Also, if all of your data is numeric, it might be better to convert to a
matrix before doing your calculations. For example:
x <- as.matrix(x)
x[is.nan(x)] <- NA_real_
I'd also suggest this same solution for the other question you posted,
x[x == 0] <- NA
On Thu, Sep 2, 2021 at 10:01 AM Luigi Marongiu <marongiu.luigi at
gmail.com>
wrote:
> Sorry,
> still I don't get it:
> ```
> > dim(df)
> [1] 302 626
> > # clean
> > df <- lapply(x, function(xx) {
> + xx[is.nan(xx)] <- NA
> + xx
> + })
> > dim(df)
> NULL
> ```
>
> On Thu, Sep 2, 2021 at 3:47 PM Andrew Simmons <akwsimmo at gmail.com>
wrote:
> >
> > You removed the second line 'xx' from the function, put it
back and it
> should work
> >
> > On Thu, Sep 2, 2021, 09:45 Luigi Marongiu <marongiu.luigi at
gmail.com>
> wrote:
> >>
> >> `data[sapply(data, is.nan)] <- NA` is a nice compact command,
but I
> >> still get NaN when using the summary function, for instance one of
the
> >> columns give:
> >> ```
> >> Min. : NA
> >> 1st Qu.: NA
> >> Median : NA
> >> Mean :NaN
> >> 3rd Qu.: NA
> >> Max. : NA
> >> NA's :110
> >> ```
> >> I tried to implement the second solution but:
> >> ```
> >> df <- lapply(x, function(xx) {
> >> xx[is.nan(xx)] <- NA
> >> })
> >> > str(df)
> >> List of 1
> >> $ sd_ef_rash_loc___palm: logi NA
> >> ```
> >> What am I getting wrong?
> >> Thanks
> >>
> >> On Thu, Sep 2, 2021 at 3:30 PM Andrew Simmons <akwsimmo at
gmail.com>
> wrote:
> >> >
> >> > Hello,
> >> >
> >> >
> >> > I would use something like:
> >> >
> >> >
> >> > x <- c(1:5, NaN) |> sample(100, replace = TRUE) |>
matrix(10, 10) |>
> as.data.frame()
> >> > x[] <- lapply(x, function(xx) {
> >> > xx[is.nan(xx)] <- NA_real_
> >> > xx
> >> > })
> >> >
> >> >
> >> > This prevents attributes from being changed in 'x',
but accomplishes
> the same thing as you have above, I hope this helps!
> >> >
> >> > On Thu, Sep 2, 2021 at 9:19 AM Luigi Marongiu <
> marongiu.luigi at gmail.com> wrote:
> >> >>
> >> >> Hello,
> >> >> I have some NaN values in some elements of a dataframe
that I would
> >> >> like to convert to NA.
> >> >> The command `df1$col[is.nan(df1$col)]<-NA` allows to
work
> column-wise.
> >> >> Is there an alternative for the global modification at
once of all
> >> >> instances?
> >> >> I have seen from
> >> >>
>
https://stackoverflow.com/questions/18142117/how-to-replace-nan-value-with-zero-in-a-huge-data-frame/18143097#18143097
> >> >> that once could use:
> >> >> ```
> >> >>
> >> >> is.nan.data.frame <- function(x)
> >> >> do.call(cbind, lapply(x, is.nan))
> >> >>
> >> >> data123[is.nan(data123)] <- 0
> >> >> ```
> >> >> replacing o with NA, but I got
> >> >> ```
> >> >> str(df)
> >> >> > logi NA
> >> >> ```
> >> >> when modifying my dataframe df.
> >> >> What would be the correct syntax?
> >> >> Thank you
> >> >>
> >> >>
> >> >>
> >> >> --
> >> >> Best regards,
> >> >> Luigi
> >> >>
> >> >> ______________________________________________
> >> >> R-help at r-project.org mailing list -- To UNSUBSCRIBE
and more, see
> >> >> https://stat.ethz.ch/mailman/listinfo/r-help
> >> >> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> >> >> and provide commented, minimal, self-contained,
reproducible code.
> >>
> >>
> >>
> >> --
> >> Best regards,
> >> Luigi
>
>
>
> --
> Best regards,
> Luigi
>
[[alternative HTML version deleted]]
Thank you! On Thu, Sep 2, 2021 at 4:17 PM Andrew Simmons <akwsimmo at gmail.com> wrote:> > It seems like you might've missed one more thing, you need the brackets next to 'x' to get it to work. > > > 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 > }) > > Also, if all of your data is numeric, it might be better to convert to a matrix before doing your calculations. For example: > > x <- as.matrix(x) > x[is.nan(x)] <- NA_real_ > > I'd also suggest this same solution for the other question you posted, > > x[x == 0] <- NA > > On Thu, Sep 2, 2021 at 10:01 AM Luigi Marongiu <marongiu.luigi at gmail.com> wrote: >> >> Sorry, >> still I don't get it: >> ``` >> > dim(df) >> [1] 302 626 >> > # clean >> > df <- lapply(x, function(xx) { >> + xx[is.nan(xx)] <- NA >> + xx >> + }) >> > dim(df) >> NULL >> ``` >> >> On Thu, Sep 2, 2021 at 3:47 PM Andrew Simmons <akwsimmo at gmail.com> wrote: >> > >> > You removed the second line 'xx' from the function, put it back and it should work >> > >> > On Thu, Sep 2, 2021, 09:45 Luigi Marongiu <marongiu.luigi at gmail.com> wrote: >> >> >> >> `data[sapply(data, is.nan)] <- NA` is a nice compact command, but I >> >> still get NaN when using the summary function, for instance one of the >> >> columns give: >> >> ``` >> >> Min. : NA >> >> 1st Qu.: NA >> >> Median : NA >> >> Mean :NaN >> >> 3rd Qu.: NA >> >> Max. : NA >> >> NA's :110 >> >> ``` >> >> I tried to implement the second solution but: >> >> ``` >> >> df <- lapply(x, function(xx) { >> >> xx[is.nan(xx)] <- NA >> >> }) >> >> > str(df) >> >> List of 1 >> >> $ sd_ef_rash_loc___palm: logi NA >> >> ``` >> >> What am I getting wrong? >> >> Thanks >> >> >> >> On Thu, Sep 2, 2021 at 3:30 PM Andrew Simmons <akwsimmo at gmail.com> wrote: >> >> > >> >> > Hello, >> >> > >> >> > >> >> > I would use something like: >> >> > >> >> > >> >> > x <- c(1:5, NaN) |> sample(100, replace = TRUE) |> matrix(10, 10) |> as.data.frame() >> >> > x[] <- lapply(x, function(xx) { >> >> > xx[is.nan(xx)] <- NA_real_ >> >> > xx >> >> > }) >> >> > >> >> > >> >> > This prevents attributes from being changed in 'x', but accomplishes the same thing as you have above, I hope this helps! >> >> > >> >> > On Thu, Sep 2, 2021 at 9:19 AM Luigi Marongiu <marongiu.luigi at gmail.com> wrote: >> >> >> >> >> >> Hello, >> >> >> I have some NaN values in some elements of a dataframe that I would >> >> >> like to convert to NA. >> >> >> The command `df1$col[is.nan(df1$col)]<-NA` allows to work column-wise. >> >> >> Is there an alternative for the global modification at once of all >> >> >> instances? >> >> >> I have seen from >> >> >> https://stackoverflow.com/questions/18142117/how-to-replace-nan-value-with-zero-in-a-huge-data-frame/18143097#18143097 >> >> >> that once could use: >> >> >> ``` >> >> >> >> >> >> is.nan.data.frame <- function(x) >> >> >> do.call(cbind, lapply(x, is.nan)) >> >> >> >> >> >> data123[is.nan(data123)] <- 0 >> >> >> ``` >> >> >> replacing o with NA, but I got >> >> >> ``` >> >> >> str(df) >> >> >> > logi NA >> >> >> ``` >> >> >> when modifying my dataframe df. >> >> >> What would be the correct syntax? >> >> >> Thank you >> >> >> >> >> >> >> >> >> >> >> >> -- >> >> >> Best regards, >> >> >> Luigi >> >> >> >> >> >> ______________________________________________ >> >> >> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see >> >> >> https://stat.ethz.ch/mailman/listinfo/r-help >> >> >> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html >> >> >> and provide commented, minimal, self-contained, reproducible code. >> >> >> >> >> >> >> >> -- >> >> Best regards, >> >> Luigi >> >> >> >> -- >> Best regards, >> Luigi-- Best regards, Luigi
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? is it because of (or, "is the clue...") this in the "Value:" section of : ?"[<-.data.frame" ---- For '[<-', '[[<-' and '$<-', a data frame. ---- ? cheers, Greg