R 中的哪些函数可以递归 "reduce" 数据帧的行?
What functions in R can recursively "reduce" the rows of a dataframe?
R 中的哪些函数可以递归地“减少”数据帧的行数?我正在考虑一个像 Reduce()
这样的函数,但它接受一个数据帧而不是一个向量,以及一个接受数据帧的每一行和一个累加器的函数。
考虑以下示例,该示例创建了一个数据框,其中包含购买清单的价格和数量,并使用 Reduce()
计算 运行 总成本。
purchases = data.frame(
price = c(1.50, 1.75, 2.00, 2.10, 1.80),
quantity = c(100, 80, 50, 20, 90)
)
print(purchases)
#> price quantity
#> 1 1.50 100
#> 2 1.75 80
#> 3 2.00 50
#> 4 2.10 20
#> 5 1.80 90
purchase_costs <- purchases$quantity * purchases$price
print(purchase_costs)
#> [1] 150 140 100 42 162
total_cost <- Reduce(
function(total_cost, cost) { total_cost + cost },
purchase_costs,
accumulate = TRUE
)
print(total_cost)
#> [1] 150 290 390 432 594
由 reprex package (v2.0.1)
创建于 2022-02-01
R 中类似于 Reduce()
的哪些函数可以通过递归处理数据框中的每次购买而不是成本向量中的每次购买来计算此 运行 总成本?这样的 Reduce()
函数可能类似于以下内容:
total_cost <- Reduce(
function(total_cost, purchase) { total_cost + purchase["quantity"] * purchase["price"] },
purchases,
accumulate = TRUE
)
这个怎么样?
library(tidyverse)
purchases %>%
mutate(cost = price * quantity) %>%
mutate(total_cost = cost + lag(cumsum(cost)))
price quantity cost total_cost
1 1.50 100 150 NA
2 1.75 80 140 290
3 2.00 50 100 390
4 2.10 20 42 432
5 1.80 90 162 594
Reduce
本身不会像您想要的那样运行 row-wise:它在简单矢量或 list
上运行良好,但在帧的行上运行不佳。
试试这个 frame-aware 函数:
Reduce_frame <- function(data, expr, init) {
expr <- substitute(expr)
out <- rep(init[1][NA], nrow(data))
for (rn in seq_len(nrow(data))) {
out[rn] <- init <- eval(expr, envir = data[rn,])
}
out
}
Reduce_frame(purchases, init + quantity*price, init=0)
# [1] 150 290 390 432 594
R 中的哪些函数可以递归地“减少”数据帧的行数?我正在考虑一个像 Reduce()
这样的函数,但它接受一个数据帧而不是一个向量,以及一个接受数据帧的每一行和一个累加器的函数。
考虑以下示例,该示例创建了一个数据框,其中包含购买清单的价格和数量,并使用 Reduce()
计算 运行 总成本。
purchases = data.frame(
price = c(1.50, 1.75, 2.00, 2.10, 1.80),
quantity = c(100, 80, 50, 20, 90)
)
print(purchases)
#> price quantity
#> 1 1.50 100
#> 2 1.75 80
#> 3 2.00 50
#> 4 2.10 20
#> 5 1.80 90
purchase_costs <- purchases$quantity * purchases$price
print(purchase_costs)
#> [1] 150 140 100 42 162
total_cost <- Reduce(
function(total_cost, cost) { total_cost + cost },
purchase_costs,
accumulate = TRUE
)
print(total_cost)
#> [1] 150 290 390 432 594
由 reprex package (v2.0.1)
创建于 2022-02-01R 中类似于 Reduce()
的哪些函数可以通过递归处理数据框中的每次购买而不是成本向量中的每次购买来计算此 运行 总成本?这样的 Reduce()
函数可能类似于以下内容:
total_cost <- Reduce(
function(total_cost, purchase) { total_cost + purchase["quantity"] * purchase["price"] },
purchases,
accumulate = TRUE
)
这个怎么样?
library(tidyverse)
purchases %>%
mutate(cost = price * quantity) %>%
mutate(total_cost = cost + lag(cumsum(cost)))
price quantity cost total_cost
1 1.50 100 150 NA
2 1.75 80 140 290
3 2.00 50 100 390
4 2.10 20 42 432
5 1.80 90 162 594
Reduce
本身不会像您想要的那样运行 row-wise:它在简单矢量或 list
上运行良好,但在帧的行上运行不佳。
试试这个 frame-aware 函数:
Reduce_frame <- function(data, expr, init) {
expr <- substitute(expr)
out <- rep(init[1][NA], nrow(data))
for (rn in seq_len(nrow(data))) {
out[rn] <- init <- eval(expr, envir = data[rn,])
}
out
}
Reduce_frame(purchases, init + quantity*price, init=0)
# [1] 150 290 390 432 594