根据数据框中其他变量的动态数量创建新变量

时间:2019-07-11 18:39:41

标签: r loops dataframe

我在为每个Entity_x_charge列创建一个Entity_x_delivery列时遇到了麻烦,因为有时在过滤了月份数据之后,只有1个,而有时却交付了2或3个实体。

从理论上讲,我应该能够为交货的每一列创建一列用于收费。但是,明确命名它们并不起作用,因为它们并不总是存在。

我在excel中有此功能,但每个实体都有静态列。在R中,实体将具有隐式的NA,而该隐式却难以克服

出于隐私原因,我无法共享数据,但到目前为止的代码如下:

library(tidyverse)
library(readxl)

headgate_totals_lateral_month <- headgate_data_entry %>%
  filter(!Entity == "DO NOT CHARGE", Month == current_month,
         Year == current_year) %>% group_by(Lateral) %>%
  summarise(total_lateral_deliveries_month = sum(AF))

headgate_totals_entity_month <- headgate_data_entry %>%
  filter(!Entity == "DO NOT CHARGE", Month == current_month,
         Year == current_year) %>% group_by(Lateral, Entity) %>%
         summarise(lateral_deliveries_month = sum(AF)) %>%
         spread(key=Entity, value =lateral_deliveries_month)


charges_lateral_month <- swp_charges_month %>%
  filter(Year== current_year, Month == current_month) %>%
         group_by(Lateral) %>% summarise(total_lateral_charges_month =                   
                                         sum(`State Aqueduct Deliveries`))

step_1_data <- Reduce(function(x,y) merge(x=x, y=y, by = "Lateral"),
                      list(headgate_totals_entity_month, 
                      headgate_totals_lateral_month, charges_lateral_month))  
step_1_data$equal_percent_delivery <- step_1_data$total_lateral_deliveries_month / 
                                      step_1_data$total_lateral_charges_month

我需要的例子

step_1_data$ews_id_charges <- step_1_data$`EWS ID`/
                              step_1_data$equal_percent_delivery

step_1_data$tlbwsd_charges <- step_1_data$total_lateral_charges_month -                       
                              step_1_data$ews_id_charges

所交付的每个实体的专栏。它们落在第一列step_1_data Lateraltotal_lateral_deliveries之间,deliveries / equal_percent根据交付的实体数更改位置。

对于每个等于tlbwsd的交付实体,我需要1列结尾_charges,然后为total_lateral_charges_month - SUM(entity_charges)等于{{1}}的列

任何有关更改代码的建议都会受到欢迎。我仍在学习中...

0 个答案:

没有答案