Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 201919

R - object not found error when it exists

$
0
0

I am banging my head trying to understand why "R" cannot find a variable that is definitely in my dataframe. Hoping someone can help.

Here is the dput for "DF.1" in my code below:

library("dplyr")
library("stringr")
DF.1 <- structure(list(`ID` = c("APP-5XUEJHC1XN-2019", 
    "APP-AVO1K5F33B-2019", "APP-J12JZHOWTM-2019", "APP-VROJDQSZ3P-2019", 
    "APP-00AURK6GEP-2019", "APP-00VACS4YZI-2018", "APP-00W7N0XXSO-2019", 
    "APP-01AQMLSHX6-2019", "APP-021R8JXC6O-2018", "APP-022XIXHHIQ-2019", 
    "APP-025ZNBC262-2018", "APP-02IUB6YJ05-2019", "APP-02PSFXZI1U-2019", 
    "APP-02TZN2M3JT-2019", "APP-034IPEAN7E-2018", "APP-03XWZT90ZW-2018", 
    "APP-040I2UPEEI-2019", "APP-0442F1YUCB-2019", "APP-04DKWB5EF3-2019", 
    "APP-04E58XMYDH-2018"), `Observations` = c("Single", 
    "Single", "Single", "Single", NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, NA)), class = c("tbl_df", "tbl", 
    "data.frame"), row.names = c(NA, -20L))

DF.2 <- DF.1 %>% dplyr::mutate(
    "New Var" = case_when(
        str_detect(tolower(`Observations`), "single") ~ "Single Protocol",
        str_detect(tolower(`Observations`), "multiple") |
            !(str_detect(tolower(`Observations`), paste(c("single", "multiple"), collapse = '|'))) |
            is.na(`Observations`) ~ "Multiple Protocol"))

When I run the above code, I get the following error:

Error in eval_tidy(pair$lhs, env = default_env) : object 'Observations' not found

The variable is in the dataframe, so I am wondering if there is a conflict with either case_when or str_detect.


Viewing all articles
Browse latest Browse all 201919

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>