[R] substitute column data frame based on name stored in variable in r

Luigi Marongiu m@rong|u@|u|g| @end|ng |rom gm@||@com
Mon Aug 9 21:22:40 CEST 2021


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



More information about the R-help mailing list