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

Ivan Krylov kry|ov@r00t @end|ng |rom gm@||@com
Mon Aug 9 17:18:43 CEST 2021


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



More information about the R-help mailing list