使用 R 中单独列中提供的方程和参数计算新变量
The calculation of new variable using the equation and parameters provided in separate columns in R
我有一个包含五列的数据框,需要它们来计算新变量 (y):
- 变量 x(数字)
- 公式 1(字符)
- 等式 1 的参数 a
- 等式 1 的参数 b
- 方程式 1 的参数 c
data <- data.frame(
x = c(3,4,5),
equation = c("5*x + a * 2 - pi * b", "10*x + a - log(b)", "-x + a + c"),
a = c(1,4,8),
b = c(0.1,4,-8),
c =c(1.5,0.4,18),
y = c(NA, NA, NA),
y_expected = c(16.69, 42.61, 21.00))
现在,对于每一行,我想分别使用等式 1 和 a、b 和 c 参数来计算 y(请参阅手动计算的预期 y 值)。我用下面的代码来做,但结果不正确。这里发生的是 y 变量是使用第 3 列中的方程式计算的。
library(dplyr)
mutate(data, y = eval(parse(text=equation)))
x equation a b c y y_expected
1 3 5*x + a * 2 - pi * b 1 0.1 1.5 -0.5 16.69
2 4 10*x + a - log(b) 4 4.0 0.4 0.4 42.61
3 5 -x + a + c 8 -8.0 18.0 21.0 21.00
Warning message:
Problem with `mutate()` column `y`.
i `y = eval(parse(text = equation))`.
i NaNs produced
我该如何解决这个问题?我知道我可以使用 for 循环实现解决方案,但我正在寻找更优雅的解决方案。
list1 <- list()
for (i in 1:nrow(data)){
temp_df <- data[i, ]
temp_df <- mutate(temp_df, y = eval(parse(text=equation)))
list1[[i]] <- temp_df
}
do.call(rbind, list1)
x equation a b c y y_expected
1 3 5*x + a * 2 - pi * b 1 0.1 1.5 16.68584 16.69
2 4 10*x + a - log(b) 4 4.0 0.4 42.61371 42.61
3 5 -x + a + c 8 -8.0 18.0 21.00000 21.00
dplyr::rowwise()
应该可以解决问题...
library(dplyr)
data %>%
rowwise() %>%
mutate(y = eval(parse(text=equation)))
#> # A tibble: 3 x 7
#> # Rowwise:
#> x equation a b c y y_expected
#> <dbl> <chr> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 3 5*x + a * 2 - pi * b 1 0.1 1.5 16.7 16.7
#> 2 4 10*x + a - log(b) 4 4 0.4 42.6 42.6
#> 3 5 -x + a + c 8 -8 18 21 21
由 reprex package (v2.0.1)
创建于 2022-01-24
我有一个包含五列的数据框,需要它们来计算新变量 (y):
- 变量 x(数字)
- 公式 1(字符)
- 等式 1 的参数 a
- 等式 1 的参数 b
- 方程式 1 的参数 c
data <- data.frame(
x = c(3,4,5),
equation = c("5*x + a * 2 - pi * b", "10*x + a - log(b)", "-x + a + c"),
a = c(1,4,8),
b = c(0.1,4,-8),
c =c(1.5,0.4,18),
y = c(NA, NA, NA),
y_expected = c(16.69, 42.61, 21.00))
现在,对于每一行,我想分别使用等式 1 和 a、b 和 c 参数来计算 y(请参阅手动计算的预期 y 值)。我用下面的代码来做,但结果不正确。这里发生的是 y 变量是使用第 3 列中的方程式计算的。
library(dplyr)
mutate(data, y = eval(parse(text=equation)))
x equation a b c y y_expected
1 3 5*x + a * 2 - pi * b 1 0.1 1.5 -0.5 16.69
2 4 10*x + a - log(b) 4 4.0 0.4 0.4 42.61
3 5 -x + a + c 8 -8.0 18.0 21.0 21.00
Warning message:
Problem with `mutate()` column `y`.
i `y = eval(parse(text = equation))`.
i NaNs produced
我该如何解决这个问题?我知道我可以使用 for 循环实现解决方案,但我正在寻找更优雅的解决方案。
list1 <- list()
for (i in 1:nrow(data)){
temp_df <- data[i, ]
temp_df <- mutate(temp_df, y = eval(parse(text=equation)))
list1[[i]] <- temp_df
}
do.call(rbind, list1)
x equation a b c y y_expected
1 3 5*x + a * 2 - pi * b 1 0.1 1.5 16.68584 16.69
2 4 10*x + a - log(b) 4 4.0 0.4 42.61371 42.61
3 5 -x + a + c 8 -8.0 18.0 21.00000 21.00
dplyr::rowwise()
应该可以解决问题...
library(dplyr)
data %>%
rowwise() %>%
mutate(y = eval(parse(text=equation)))
#> # A tibble: 3 x 7
#> # Rowwise:
#> x equation a b c y y_expected
#> <dbl> <chr> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 3 5*x + a * 2 - pi * b 1 0.1 1.5 16.7 16.7
#> 2 4 10*x + a - log(b) 4 4 0.4 42.6 42.6
#> 3 5 -x + a + c 8 -8 18 21 21
由 reprex package (v2.0.1)
创建于 2022-01-24