if_else conditions replacing values

Hi R comm.

If the "anomaly" column is == Yes then i want to replace the "observed" column with NA

dput:

structure(list(item_id = c("AS2008", "AS2008", "AS2008", "AS2008", 
"AS2008", "AS2008", "AS2008", "AS2008", "AS2008", "AS2008", "AS2008", 
"AS2008", "AS2008", "AS2008", "AS2008", "AS2008", "AS2008", "AS2008", 
"AS2008", "AS2008", "AS2008", "AS2008", "AS2008", "AS2008", "AS2008", 
"AS2008", "AS2008", "AS2008", "AS2008", "AS2008", "AS2008", "AS2008", 
"AS2008", "AS2008", "AS2008", "AS2008", "AS2008", "AS2008", "AS2008", 
"AS2008"), date = structure(c(18262, 18263, 18264, 18265, 18267, 
18268, 18269, 18271, 18272, 18274, 18275, 18276, 18277, 18278, 
18279, 18280, 18281, 18282, 18283, 18284, 18285, 18286, 18288, 
18289, 18290, 18291, 18293, 18295, 18296, 18297, 18298, 18302, 
18303, 18304, 18305, 18308, 18309, 18310, 18311, 18313), class = "Date"), 
    observed = c(NA, 4, 1, 1, 15, 5, 11, 0, 1, 13, 8, 2, 4, 4, 
    2, 2, 13, 6, 9, 2, 2, 2, 14, -1, 1, 4, 3, 12, 3, 5, 3, 29, 
    9, 3, 4, 6, 27, 9, 4, 5), anomaly = c(`25%` = "No", `25%` = "No", 
    `25%` = "No", `25%` = "No", `25%` = "No", `25%` = "No", `25%` = "No", 
    `25%` = "No", `25%` = "No", `25%` = "No", `25%` = "No", `25%` = "No", 
    `25%` = "No", `25%` = "No", `25%` = "No", `25%` = "No", `25%` = "No", 
    `25%` = "No", `25%` = "No", `25%` = "No", `25%` = "No", `25%` = "No", 
    `25%` = "No", `25%` = "No", `25%` = "No", `25%` = "No", `25%` = "No", 
    `25%` = "No", `25%` = "No", `25%` = "No", `25%` = "No", `25%` = "Yes", 
    `25%` = "No", `25%` = "No", `25%` = "No", `25%` = "No", `25%` = "No", 
    `25%` = "No", `25%` = "No", `25%` = "No")), row.names = c(NA, 
-40L), groups = structure(list(item_id = "AS2008", .rows = structure(list(
    1:40), ptype = integer(0), class = c("vctrs_list_of", "vctrs_vctr", 
"list"))), row.names = c(NA, -1L), class = c("tbl_df", "tbl", 
"data.frame"), .drop = TRUE), class = c("grouped_df", "tbl_df", 
"tbl", "data.frame"))

also if someone have a good idea to impute the newly created NA's with some kind of imputation method please let me know :slight_smile:

Thanks!

If I store your data in the object DF, a simple way to change the observed values is

DF$observed <- ifelse(DF$anomaly == "Yes", NA, DF$observed)

Note: I had to change the dput output you posted. The anomaly column has code like c(25% = "No", 25% = "No", ...). I deleted all the 25% = occurrences so the values are simple No or Yes.

I have since edited jak123's post for him to code format his output, doing this retains the single quotes that would otherwise be hidden.

thanks both of u, tried to imputate some values for the NA's with the following code:

ah_fill <- outliers %>%
  # fit arima model to the data containing the missing values
  model(ARIMA(observed)) %>%
  # estimate "observed" for all periods
  interpolate(outliers)
ah_fill %>%
  right_join((outliers %>% select(-observed)))

but i am getting the error for the model(ARIMA()) part:

Error in UseMethod("model") : 
  no applicable method for 'model' applied to an object of class "c('grouped_df', 'tbl_df', 'tbl', 'data.frame')"

I am not sure what that means.

jak123, it might be helpful to know how to properly format code and console output that you post here.
Using proper code formatting makes the site easier to read, prevents confusion (unformatted code can get garbled by the forum software :anguished:).

Check out this FAQ to find out how — it's as easy as the click of a button! :grinning::

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.