[R] substitute column data frame based on name stored in variable in r
dw|n@em|u@ @end|ng |rom comc@@t@net
Tue Aug 10 00:12:26 CEST 2021
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:
> 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, 'vect'] <- "No"
>> Please don't quote R expressions that you want to evaluate. 'vect'
>> is just a string, like 'hello world' or 'I want to create a new column
>> named "vect" instead of accessing the second one'.
>>> Error in `[<-.data.frame`(`*tmp*`, df[[vect]] == 2, vect, value
>>> = "No") : missing values are not allowed in subscripted assignments
>>> of data frames
>> Since df[] 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 is TRUE would be
>> to use which(). Compare:
>> df[df[[vect]] == 2,]
>> df[which(df[[vect]] == 2),]
>> Best regards,
More information about the R-help