我有一套成本数据,用于推出5G移动基础设施以服务于不同的邮政编码区域:
observation <- c(1:10)
pop.d.rank <- c(1:10)
cost <- c(101:110)
all <- data.frame(observation,pop.d.rank,cost)
然后,我想通过一定数量的年度投资,看到有多少人可以获得一定数量的可用资金。我们说年度投资如下:
annual.investment <- 500
然后,我使用dplyr尝试根据成本变量将此year.investment分配给数据中的不同地理区域:
all <- all %>% group_by(pop.d.rank) %>%
mutate(capital_allocated.5G = diff(c(0, pmin(cumsum(cost), annual.investment)))) %>%
mutate(capital_percentage.5G = capital_allocated.5G / cost * 100)
然而,这里的问题是,每次对每次数据中的观察,而不是作为所有观察的总量,每年分配500的投资额。有关如何更改此代码以执行此操作的任何建议吗?
以下是数据的外观:
observation <- c(1:10)
pop.d.rank <- c(1:10)
cost <- c(101:110)
capital_allocated.5G <- c(101, 102, 103, 104, 90, 0, 0, 0, 0, 0)
capital_percentage.5G <- c(100, 100, 100, 100, 86, 0, 0, 0, 0, 0)
example.output <- data.frame(observation,pop.d.rank,cost, capital_allocated.5G, capital_percentage.5G)
答案 0 :(得分:2)
您可以使用transform
all <- transform(all,
capital_allocated.5G = diff(c(0, pmin(cumsum(cost), annual.investment))))
all <- transform(all,
capital_percentage.5G = capital_allocated.5G / cost * 100)
或者为了避免重复您的代码,您可以这样做:
all <- with(all,{
capital_allocated.5G = diff(c(0, pmin(cumsum(cost), annual.investment)))
capital_percentage.5G = capital_allocated.5G / cost * 100
return(cbind(all, capital_allocated.5G,
capital_percentage.5G))
})
他们都给你相同的输出:
## observation pop.d.rank cost capital_allocated.5G capital_percentage.5G
## 1 1 1 101 101 100.00000
## 2 2 2 102 102 100.00000
## 3 3 3 103 103 100.00000
## 4 4 4 104 104 100.00000
## 5 5 5 105 90 85.71429
## 6 6 6 106 0 0.00000
## 7 7 7 107 0 0.00000
## 8 8 8 108 0 0.00000
## 9 9 9 109 0 0.00000
## 10 10 10 110 0 0.00000
此外,如果删除group_by(pop.d.rank)