【发布时间】:2020-06-20 23:03:26
【问题描述】:
问题背景:数据框的结构如下。问题是我需要一个新列,例如 status_rank,它不是 2 个变量的 data.frame。然后需要使用基于条件的另一个变量的值来更新 status_rank。
[错字:ifelse是我用的]
使用 target.market_b/_g(“坏”、“好”值)进行条件测试。 'status' 有这些以及更多应该被忽略的内容(不是 == target.market_b/_g)。
ifelse(status %in% target.marker_b, "Bad",
ifelse(status %in% target_g, "Good", "N/A")
df$status : 字符 df$status_rank : 2 个变量的“data.frame” .. $status 字符“...” .. $status_rank chr "Bad" "Good" "N/A"
我使用 dplyr mutate 创建了一个新字段,现在我知道它会改变列“status_rank”。我现在看到 dplyr mutate 不是正确的解决方案。
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")))
发布新列创建与
df["status_rank"] <- "N/A"
然后 mutate 对“status_rank”进行操作并将其变异为 2 个观察值。需要一种更好的方法来创建新列并应用 ifelse(status %in% target.marker_b, "Bad", ifelse(status %in% target_g, "Good", "N/A")。寻找建议。
数据: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)
elseif不存在,但您可以ifelse(cond1, yes1, ifelse(cond2, yes2, no))。 (2) 样本数据会很好,否则我们只是在黑暗中刺伤。 -
刚刚添加了一个 dput() 示例数据
-
user1857373,你需要记住我们没有数据。您提供的内容为我们提供了足够的一列(也许对于框架,因为不需要其他列),但是......您的其他两个变量
target.marker_b和target_g呢?