我有一个创建数据框供另一个函数使用的函数。如果我的初始输入是偶数个参数,它会非常有效,但如果它是奇数则会中断。我根本不理解这种行为。
首先,让我们使用两个简单的数据框:three_var和four_var - 变量的数量是指“start”和“mid”变量之间的变量。
three_var
variable given total
1 start 100.0 100
2 a 0.5 50
3 b 0.7 35
4 c 1.2 42
5 mid NA 42
6 x 1.0 42
7 end NA 42
four_var
variable given total
1 start 100.0 100.0
2 a 0.5 50.0
3 b 1.7 85.0
4 c 0.2 17.0
5 d 1.5 25.5
6 mid NA 25.5
7 x 1.0 25.5
8 end NA 25.5
three_var <- structure(list(variable = c("start", "a", "b", "c", "mid", "x",
"end"), given = c(100, 0.5, 0.7, 1.2, NA, 1, NA), total = c(100,
50, 35, 42, 42, 42, 42)), .Names = c("variable", "given", "total"
), row.names = c(NA, -7L), class = "data.frame")
four_var <- structure(list(variable = c("start", "a", "b", "c", "d", "mid",
"x", "end"), given = c(100, 0.5, 1.7, 0.2, 1.5, NA, 1, NA), total = c(100,
50, 85, 17, 25.5, 25.5, 25.5, 25.5)), .Names = c("variable",
"given", "total"), row.names = c(NA, -8L), class = "data.frame")
我可以通过以下for子句传递four_var
并获得输出。
for (i in 2:nrow(four_var)) {
four_var$decrease[i] <- four_var$total[i-1] - four_var$total[i]
four_var$increase[i] <- four_var$total[i] - four_var$total[i-1]
}
four_var
variable given total decrease increase
1 start 100.0 100.0 NA NA
2 a 0.5 50.0 50.0 -50.0
3 b 1.7 85.0 -35.0 35.0
4 c 0.2 17.0 68.0 -68.0
5 d 1.5 25.5 -8.5 8.5
6 mid NA 25.5 0.0 0.0
7 x 1.0 25.5 0.0 0.0
8 end NA 25.5 0.0 0.0
然而,当我通过three_var
时,我收到错误:
for (i in 2:nrow(three_var)) {
three_var$decrease[i] <- three_var$total[i-1] - three_var$total[i]
three_var$increase[i] <- three_var$total[i] - three_var$total[i-1]
}
Error in `$<-.data.frame`(`*tmp*`, "decrease", value = c(NA, 50)) :
replacement has 2 rows, data has 7
此代码看起来不应该取决于输入的数量。我错过了什么?
答案 0 :(得分:3)
这不是在data.frame中创建新列的正确方法。它适用于four_var
,因为8/2
等于4
。 i
已被回收。您可以验证是否设置了i = 2
,运行four_var$decrease[i] <- four_var$total[i-1] - four_var$total[i]
并显示four_var
。
four_var <- structure(list(variable = c("start", "a", "b", "c", "d", "mid",
"x", "end"), given = c(100, 0.5, 1.7, 0.2, 1.5, NA, 1, NA), total = c(100,
50, 85, 17, 25.5, 25.5, 25.5, 25.5)), .Names = c("variable",
"given", "total"), row.names = c(NA, -8L), class = "data.frame")
i <- 2
four_var$decrease[i] <- four_var$total[i-1] - four_var$total[i]
# variable given total decrease
# 1 start 100.0 100.0 NA
# 2 a 0.5 50.0 50
# 3 b 1.7 85.0 NA
# 4 c 0.2 17.0 50 <- recycling
# 5 d 1.5 25.5 NA
# 6 mid NA 25.5 50 <- recycling
# 7 x 1.0 25.5 NA
# 8 end NA 25.5 50 <- recycling
three_var$decrease <- NA
three_var$increase <- NA
for (i in 2:nrow(three_var)) {
three_var$decrease[i] <- three_var$total[i-1] - three_var$total[i]
three_var$increase[i] <- three_var$total[i] - three_var$total[i-1]
}
three_var
# variable given total decrease increase
# 1 start 100.0 100 NA NA
# 2 a 0.5 50 50 -50
# 3 b 0.7 35 15 -15
# 4 c 1.2 42 -7 7
# 5 mid NA 42 0 0
# 6 x 1.0 42 0 0
# 7 end NA 42 0 0