【发布时间】:2019-04-23 14:09:48
【问题描述】:
我正在尝试遍历我的数据框的行,并根据数据框中的 Result 列将 1 添加到 Won 或 Loss 列。我需要它在每次循环代码后打印数据帧行。
我曾尝试使用 cumsum() 函数,但我需要它以结果字符(赢或输)为条件,但我每次都失败了。我还尝试了一个 for 循环,我最接近解决这个问题的方法是打印出新数据集,其中包含获胜列中的总比赛数。
for (row in 1:nrow(Dylan)) {
if( Dylan$Result == "Won") {Dylan$Won = Dylan$Won + 1}
else {Dylan$Lost = Dylan$Lost + 1}
}
Here are the errors I get:
In if (Dylan$Result == "Won") { ... :
the condition has length > 1 and only the first element will be used
2: In if (Dylan$Result == "Won") { ... :
the condition has length > 1 and only the first element will be used
我需要新的数据框来显示每个获胜和失败列的持续添加,因此列会随着行增加,而不仅仅是显示每行的获胜总数和失败总数。
【问题讨论】:
-
欢迎来到 StackOverflow!请阅读有关how to ask a good question 的信息以及如何提供reproducible example。这将使其他人更容易帮助您。
-
这行得通吗?
Dylan$Won <- cumsum(Dylan$Result == "Won") ; Dylan$Lost <- cumsum(Dylan$Result != "Won") -
完美运行!谢谢!
标签: r