【发布时间】:2020-05-07 22:04:01
【问题描述】:
当我运行以下代码时,它在控制台中正确显示,但数据框不正确,每一行都显示“IDK”
#sample data
x <- data.frame("ID" = 1:5, "action" = c("Assists","Goals", "Assists", "Goals", "Goals"), "team" ="FSU", "prev_action" = "text")
#code not working as expecting
ifelse( x$action == "Goals" & lag(x$action) == "Assists" & lag(x$team) == x$team,
x$prev_action <- "Assists",x$prev_action <- "IDK")
运行代码后控制台显示:
[1] "IDK" "Assists" "IDK" "Assists" "IDK"
...但是如果我运行这个...
print(x$prev_action)
[1] "IDK" "IDK" "IDK" "IDK" "IDK"
【问题讨论】:
标签: r dataframe if-statement