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.r...@gmail.com> wrote: > > Thanks for providing a reproducible example! > > On Mon, 9 Aug 2021 15:33:53 +0200 > Luigi Marongiu <marongiu.lu...@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 ______________________________________________ R-help@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.