Luigi Marongiu
2021-Aug-09 19:22 UTC
[R] substitute column data frame based on name stored in variable in r
Thank you! it worked fine! The only pitfall is that `NA` became `<NA>`. This is essentially the same thing anyway... On Mon, Aug 9, 2021 at 5:18 PM Ivan Krylov <krylov.r00t at gmail.com> wrote:> > Thanks for providing a reproducible example! > > On Mon, 9 Aug 2021 15:33:53 +0200 > Luigi Marongiu <marongiu.luigi at gmail.com> wrote: > > > df[df[['vect[2]']] == 2, 'vect[2]'] <- "No" > > Please don't quote R expressions that you want to evaluate. 'vect[2]' > is just a string, like 'hello world' or 'I want to create a new column > named "vect[2]" instead of accessing the second one'. > > > Error in `[<-.data.frame`(`*tmp*`, df[[vect[2]]] == 2, vect[2], value > > = "No") : missing values are not allowed in subscripted assignments > > of data frames > > Since df[[2]] containts NAs, comparisons with it also contain NAs. While > it's possible to subset data.frames with NAs (the rows corresponding to > the NAs are returned filled with NAs of corresponding types), > assignment to undefined rows is not allowed. A simple way to remove the > NAs and only leave the cases where df[[vect[2]]] == 2 is TRUE would be > to use which(). Compare: > > df[df[[vect[2]]] == 2,] > df[which(df[[vect[2]]] == 2),] > > -- > Best regards, > Ivan-- Best regards, Luigi
David Winsemius
2021-Aug-09 22:12 UTC
[R] substitute column data frame based on name stored in variable in r
On 8/9/21 12:22 PM, Luigi Marongiu wrote:> Thank you! it worked fine! The only pitfall is that `NA` became > `<NA>`. This is essentially the same thing anyway...It's not "essentially the same thing". It IS the same thing. The print function displays those '<>' characters flanking NA's when the class is factor. Type this at your console: factor(NA) -- David> > On Mon, Aug 9, 2021 at 5:18 PM Ivan Krylov <krylov.r00t at gmail.com> wrote: >> Thanks for providing a reproducible example! >> >> On Mon, 9 Aug 2021 15:33:53 +0200 >> Luigi Marongiu <marongiu.luigi at gmail.com> wrote: >> >>> df[df[['vect[2]']] == 2, 'vect[2]'] <- "No" >> Please don't quote R expressions that you want to evaluate. 'vect[2]' >> is just a string, like 'hello world' or 'I want to create a new column >> named "vect[2]" instead of accessing the second one'. >> >>> Error in `[<-.data.frame`(`*tmp*`, df[[vect[2]]] == 2, vect[2], value >>> = "No") : missing values are not allowed in subscripted assignments >>> of data frames >> Since df[[2]] containts NAs, comparisons with it also contain NAs. While >> it's possible to subset data.frames with NAs (the rows corresponding to >> the NAs are returned filled with NAs of corresponding types), >> assignment to undefined rows is not allowed. A simple way to remove the >> NAs and only leave the cases where df[[vect[2]]] == 2 is TRUE would be >> to use which(). Compare: >> >> df[df[[vect[2]]] == 2,] >> df[which(df[[vect[2]]] == 2),] >> >> -- >> Best regards, >> Ivan > >