【发布时间】:2021-10-05 15:24:33
【问题描述】:
我有一个数据框,我需要根据另一列中变量的值在不同的列中进行单个观察。我是编程新手,很难找到可以遵循的具体示例。当 Item 值为 1615 时,Epoch 应该是 261,并且在 Item 的所有其他级别,Epoch 应该不会改变。数据框名称是 myevents。以下是数据示例:
| Item | Epoch |
|---|---|
| 1612 | 260 |
| 1613 | 0 |
| 1614 | 0 |
| 1615 | 0 |
| 1616 | 0 |
| 1617 | 0 |
| 1618 | 262 |
| 1619 | 0 |
下面是输出的样子:
| Item | Epoch |
|---|---|
| 1612 | 260 |
| 1613 | 0 |
| 1614 | 0 |
| 1615 | 261 |
| 1616 | 0 |
| 1617 | 0 |
| 1618 | 262 |
| 1619 | 0 |
这是我尝试过的:
myevents$Epoch=ifelse(myevents$Item==1615, myevents$Epoch==261, myevents$Epoch==myevents$Epoch)
myevents$Epoch=ifelse(myevents$Item==1615, myevents$Epoch==261, myevents$Epoch)
myevents$Epoch <- with(myevents, ifelse(myevents$Item==1615, myevents2$Epoch==261, myevents$Epoch==myevents$Epoch))
myevents$Epoch <- with(myevents, if(myevents$Item==1615, myevents$Epoch==261))
以上都不起作用,有些实际上用所有TRUE 或FALSE 字符替换了该列。我确保所有数字都被视为数字,并且为了以防万一,我确实尝试将它们用单引号括起来。我也尝试过仅使用if 的表单,但我无法弄清楚如何以实际运行的方式使用它。非常感谢任何帮助!
【问题讨论】:
标签: r if-statement data-manipulation