0
votes

Problem context: data frame has structure as follows. The issue is I need a new column, e.g., status_rank, which is not a data.frame of 2 variables. Then need to update the status_rank with values based on another variable for a conditional.

[typo: ifelse is what I used]

Tried conditional with target.market_b/_g ("Bad", "Good" values). 'status' has these plus more that shall be ignored (not == target.market_b/_g).

ifelse(status %in% target.marker_b, "Bad",
       ifelse(status %in% target_g, "Good", "N/A")

df$status : chr df$status_rank : 'data.frame' of 2 variables .. $status chr "..." .. $status_rank chr "Bad" "Good" "N/A"

I used a dplyr mutate to create a new field and now I know it mutates the column 'status_rank'. I see now that dplyr mutate is not the correct solution.

df$status_rank <- df %>% 
  select(status, status_rank) %>%
     mutate(status_rank = ifelse(status %in% target.marker_b, "Bad",
       ifelse(status %in% target_g, "Good", "N/A")))

Issued new column creation with

df["status_rank"] <- "N/A"

Then mutate operated on 'status_rank' and mutated it into 2 observations. Need a better way to create a new column and apply ifelse(status %in% target.marker_b, "Bad", ifelse(status %in% target_g, "Good", "N/A"). Looking for suggestions.

Data: dput(df$status)

"Current", "Fully Paid", "Current", "Fully Paid", "Charged Off", "Current", "Current", "Fully Paid", "Current", "Fully Paid", "Charged Off", "Late (31-120 days)", "Current", "Fully Paid", "Current", "Fully Paid", "Charged Off", "Current", "Current", "Fully Paid", "Late (31-120 days)", "Fully Paid", "Charged Off", "Current"
1
(1) elseif doesn't exist, though you can ifelse(cond1, yes1, ifelse(cond2, yes2, no)). (2) Sample data would be good, as otherwise we're just stabbing in the dark. - r2evans
just added a dput() sample data - user1857373
user1857373, you need to remember that we have no data. What you provided gives us enough for that one column (and perhaps for the frame, since no other columns are required), but ... what about your other two variables target.marker_b and target_g? - r2evans

1 Answers

1
votes

It's a little bit difficult to understand what you're talking about without a reproducible example, however the case_when function from dplyr may interest you:

# Untested code (as no sample data was given)

library(dplyr)

df %>% 
  mutate(status_rank = case_when(
    status %in% target.marker_b ~ "Bad",
    status %in% target_g ~ "Good",
    TRUE ~ "N/A" # Default condition to catch other cases
  ))

If you want an NA value rather than the character "N/A" then you don't need the default condition. Rows which don't meet any of the conditions will be given the value NA_character_.

i.e

df %>% 
  mutate(status_rank = case_when(
    status %in% target.marker_b ~ "Bad",
    status %in% target_g ~ "Good"
  ))