R-使用dplyr mutate分配总数的一部分
我有一套5G移动基础设施用于服务不同邮政编码地区的成本数据:R-使用dplyr mutate分配总数的一部分,r,dplyr,cumulative-sum,R,Dplyr,Cumulative Sum,我有一套5G移动基础设施用于服务不同邮政编码地区的成本数据: observation <- c(1:10) pop.d.rank <- c(1:10) cost <- c(101:110) all <- data.frame(observation,pop.d.rank,cost) observation您可以使用transform all <- transform(all, capital_allocated.5G = diff(c(
observation <- c(1:10)
pop.d.rank <- c(1:10)
cost <- c(101:110)
all <- data.frame(observation,pop.d.rank,cost)
observation您可以使用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)
此外,如果您删除group\u by(pop.d.rank)
您可以使用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)
此外,如果删除groupby(pop.d.rank)
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)
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