vendredi 7 février 2020

evaluate expression within a data frame

My data frame looks like this:

df <- data.frame(Name=c("John","Paul","Bob"), Age=c("20","30_&_5","40"))

  Name    Age
1 John     20
2 Paul 30_&_5
3  Bob     40

In the 'Age' column, I sometimes have an operation to perform (see line 2, where '&' can be understood as '+'), in order to get:

  Name    Age
1 John     20
2 Paul     35
3  Bob     40

What I tried:

library(stringr)

ifelse(
    str_detect(df$Age, "[[:digit:]]+_[+]_[[:digit:]]+"),
    {
        df$Age <- as.character(df$Age)
        new_age <- unlist(strsplit(df$Age,"_"))
        as.numeric(new_age[1]) + as.numeric(new_age[3])
    },
    df$Age
)

[1] "20" NA   "40"

Warning message:
In ifelse(str_detect(df$Age, "[[:digit:]]+_[+]_[[:digit:]]+"), { :
  NAs introduced by coercion

However, when I remove the first line the code works as expected.

EDIT: My problem is not about performing the operation. This is just an example that reproduces the error message I get.

Aucun commentaire:

Enregistrer un commentaire