【发布时间】:2021-10-08 23:35:24
【问题描述】:
我有一个类似的数据框:
date <- as.Date(c('2010-11-1','2010-11-2','2010-11-3','2010-11-4','2010-11-5','2010-11-6','2010-11-7','2010-11-8','2010-11-9','2010-11-10'))
precipitation <- c(0, 11, 12,3,0,0,0,7,9,10)
snowheight <- c(5,7,56,32, 11, 24, 70,8, 13, 11)
temperature <- c(-5, -2, 0, 0.4, -1, 5,6,4, 9, 10)
df <- data.frame(date, precipitation, snowheight, temperature)
我正在尝试根据以下条件为每个数据样本创建一个具有(0 和 1)的二分变量:
- 如果雪高 > 10,我们继续以下条件。否则将 NA 分配给二分变量。
- 如果降水 =
- 如果降水 > 0 和温度 > 0,我们分配 1。
- 如果降水 > 0 且温度
我发现 ifelse 与嵌套条件一起使用很容易,但是,由于条件部分重叠,这将不起作用。我想到的下一件事是使用 for 循环并检查每一行。这是我想出来的:
for (i in df){
if (snowheight > 10 && rain > 0){
if (temperature > 0){
df$dicht <- 1
} else {
df$dicht <- 0
}
} else {
df$dicht <- NA
}
}
当我像这样运行代码时,我得到一个变量“dicht”,完全用“NA”填充。我想看看这是什么类型的错误,但我不知道如何解决它。似乎以这种方式编写的整个“dicht”变量都被分配了值而不是索引行。我试过这样:
for (i in df){
if (snowheight > 10 && rain > 0){
if (temperature > 0){
df$dicht [i] <- 1
} else {
df$dicht [i] <- 0
}
} else {
df$dicht [i] <- NA
}
}
但是,我收到以下错误:
$<-.data.frame(*tmp*, "dicht", value = c(NA, NA, NA, NA, 中的错误: 替换有14923行,数据有10
感谢任何帮助。提前致谢。
【问题讨论】:
标签: r for-loop if-statement conditional-statements