Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 简化多行计算_R_Dplyr - Fatal编程技术网

R 简化多行计算

R 简化多行计算,r,dplyr,R,Dplyr,我有一个数据框架,其中包含一段时间内特定产品记录的数量和预订量的基本描述性统计数据(总和、平均值、最大值和计数)。该产品可以是新产品或旧产品 在我的示例中,我用于表示列的符号如下所示: 表示法: N and O = New and Old respectively P1 and P2 = Period 1 and Period2 respectively B and Q = Bookings and Quantity respectively S and A = Sum and Average

我有一个数据框架,其中包含一段时间内特定产品记录的数量和预订量的基本描述性统计数据(总和、平均值、最大值和计数)。该产品可以是新产品或旧产品

在我的示例中,我用于表示列的符号如下所示:

表示法:

N and O = New and Old respectively
P1 and P2 = Period 1 and Period2 respectively
B and Q = Bookings and Quantity respectively
S and A = Sum and Average respectively.
P1 = Period 1
P2 = Period 2
P12 = Period 1 + Period 2 = Total Period
n = count 
M = Max
dput(Master_Final)
structure(list(Country_SL6 = c("United States", "United States", 
"United States", "United States", "United Kingdom", "United Kingdom"
), Company.Name = c("Mass Incorp.", "Mass Incorp.", "Mass Incorp.", 
"Mass Incorp.", "Texan Incorp.", "Texan Incorp."), Family_Type = c("N", 
"N", "O", "O", "O", "O"), Ship_Period = c("P1", "P2", "P1", "P2", 
"P1", "P2"), Q_S = c(1, 15, 4633, 57317, 251, 1205), B_S = c(1157, 
26958.4, 3736290.43, 6144393.02, 171699, 1022155.1), Q_A = c(1, 
2.14285714285714, 71.2769230769231, 707.617283950617, 25.1, 34.4285714285714
), B_A = c(1157, 3851.2, 57481.3912307692, 75856.7039506173, 
17169.9, 29204.4314285714), Q_M = c(1, 8, 1940, 11000, 234, 617
), B_M = c(1157, 17980, 1270354, 1463415.25, 128258, 341293.55
), n = c(1, 7, 65, 81, 10, 35)), .Names = c("Country_SL6", "Company.Name", 
"Family_Type", "Ship_Period", "Q_S", "B_S", "Q_A", "B_A", "Q_M", 
"B_M", "n"), row.names = c(NA, 6L), class = "data.frame")
Top_Act_Final<- Master_Final %>%
  #Spread out the columns so that we can do row-wise operations
  gather(key = Column, value = value,Q_S:n) %>%
  unite(P_Column, Ship_Period,Column,sep="_") %>%
  unite(F_P_Column, Family_Type,P_Column,sep="_") %>%
  spread(F_P_Column, value) %>%
  dplyr::rowwise(.)%>%
  #Notation:
  #N vs. O = New or Old
  #P1 vs. P2 = Period 1 vs. Period2
  #B vs. Q = Bookings vs. Quantity
  #S vs. A = Sum vs. Average

  dplyr::mutate(P12_B_S = sum(N_P1_B_S,O_P1_B_S,N_P2_B_S,O_P2_B_S,na.rm=TRUE),
                P12_Q_S = sum(N_P1_Q_S,O_P1_Q_S,N_P2_Q_S,O_P2_Q_S,na.rm=TRUE),
                P12_B_A = sum(N_P1_B_A*N_P1_n,  O_P1_B_A*O_P1_n,  N_P2_B_A*N_P2_n,  O_P2_B_A*O_P2_n,  na.rm = TRUE)/sum(N_P1_n,O_P1_n,N_P2_n,O_P2_n,na.rm=TRUE),
                P12_Q_A = sum(N_P1_B_A*N_P1_n,  O_P1_B_A*O_P1_n,  N_P2_B_A*N_P2_n,  O_P2_B_A*O_P2_n,  na.rm = TRUE)/sum(N_P1_n,O_P1_n,N_P2_n,O_P2_n,na.rm=TRUE),

                P2_B_A = sum(N_P2_B_A*N_P2_n,  O_P2_B_A*O_P2_n,  na.rm = TRUE)/sum(N_P2_n, O_P2_n,na.rm=TRUE),
                P2_Q_A = sum( N_P2_Q_A*N_P2_n,  O_P2_Q_A*O_P2_n,  na.rm = TRUE)/sum(N_P2_n, O_P2_n,na.rm=TRUE),
                P1_B_A = sum(N_P1_B_A*N_P1_n,  O_P1_B_A*O_P1_n,  na.rm = TRUE)/sum(N_P1_n, O_P1_n,na.rm=TRUE),
                P1_Q_A = sum( N_P1_Q_A*N_P1_n,  O_P1_Q_A*O_P1_n,  na.rm = TRUE)/sum(N_P1_n, O_P1_n,na.rm=TRUE),
                P1_B_S = sum(N_P1_B_S,  O_P1_B_S,  na.rm = TRUE),                  
                P1_Q_S = sum(N_P1_Q_S,  O_P1_Q_S,  na.rm = TRUE),                  

                P2_B_S = sum(N_P2_B_S,  O_P2_B_S,  na.rm = TRUE),                  
                P2_Q_S = sum(N_P2_Q_S,  O_P2_Q_S,  na.rm = TRUE),      

                P2_B_M = max(N_P2_B_M,O_P2_B_M, na.rm = TRUE),
                P2_Q_M = max(N_P2_Q_M,O_P2_Q_M, na.rm = TRUE),
                P1_n = sum(N_P1_n,O_P1_n,na.rm = TRUE),
                P2_n = sum(N_P2_n,O_P2_n,na.rm = TRUE))

#replace NaN or -Inf with NA
Top_Act_Final[is.nan(Top_Act_Final$P2_B_A),"P2_B_A"]<-NA
Top_Act_Final[is.nan(Top_Act_Final$P2_Q_A),"P2_Q_A"]<-NA
Top_Act_Final[is.nan(Top_Act_Final$P2_B_M),"P2_B_M"]<-NA
Top_Act_Final[is.nan(Top_Act_Final$P2_Q_M),"P2_Q_M"]<-NA

Top_Act_Final<-     Top_Act_Final %>%

  dplyr::mutate(P12_B_A =sum(P1_B_A*P1_n,P2_B_A*P2_n,na.rm=TRUE)/(P1_n+P2_n),
                P12_Q_A =sum(P1_Q_A*P1_n,P2_Q_A*P2_n,na.rm=TRUE)/(P1_n+P2_n),
                P12_B_S =sum(P1_B_S,P2_B_S,na.rm=TRUE),
                P12_Q_S =sum(P2_Q_S,P1_Q_S,na.rm=TRUE)) %>%

  dplyr::mutate(B_A_c = P2_B_A>P12_B_A,
                Q_A_c = P2_Q_A>P12_Q_A,
                B_M_c = P2_B_M>P12_B_A,
                Q_M_c = P2_Q_M>P12_Q_A,
                N_O_A_c = N_P2_B_A>O_P2_B_A,
                N_O_S_c = N_P2_B_S>O_P2_B_S) %>%

  dplyr::mutate(Comparison_p1_p2 = sum(B_A_c, Q_A_c, B_M_c, Q_M_c, na.rm = TRUE),
                Comparison_p2new_P2old = sum(N_O_A_c,N_O_S_c)) %>%

  dplyr::select(Country_SL6, Company.Name, Comparison_p1_p2, Comparison_p2new_P2old)
因此,N_P2_B_A意味着Np时期2P2)销售的新产品(N),并计算Bookings(B)A平均值(A)。同样,P12_Q_A将意味着A平均值(A)Q总销售量(Q)Period(P12)

我这样做是为了缩短列名并尽量减少记住它们的负担。自动完成在RStudio中不适用于列名

我想做什么: 我的代码的目标是计算
比较\u p1\u p2
,其中我将添加以下四个逻辑的输出。对于每一个问题,如果是,答案都是
TRUE
,如果不是
FALSE

a) 是P2的平均预订量>P1+P2的平均预订量(即总期间)

b) 是P2中的数量平均值>P1+P2中的预订平均值(即总时段)

c) 是P2中的最大预订量>P1+P2中的平均预订量(即总时段)

d) 是P2中的最大数量>P1+P2中的平均数量(即总周期)

同样,我会比较P2中销售的新产品和旧产品。我将此列称为
Comparison\u p2new\u P2old
。我将添加以下输出:

a) P2中销售新产品的平均预订量>P2中销售旧产品的平均预订量

b) P2中销售新产品的平均数量>P2中销售旧产品的平均数量

我在哪里需要你的帮助 现在,我需要帮助简化此代码。虽然我的代码可以工作,但我不确定如何通过使用矢量化操作来简化它。我最近从C++/Java转换过来,所以使用R的矢量化对我来说非常困难

输入文件:

N and O = New and Old respectively
P1 and P2 = Period 1 and Period2 respectively
B and Q = Bookings and Quantity respectively
S and A = Sum and Average respectively.
P1 = Period 1
P2 = Period 2
P12 = Period 1 + Period 2 = Total Period
n = count 
M = Max
dput(Master_Final)
structure(list(Country_SL6 = c("United States", "United States", 
"United States", "United States", "United Kingdom", "United Kingdom"
), Company.Name = c("Mass Incorp.", "Mass Incorp.", "Mass Incorp.", 
"Mass Incorp.", "Texan Incorp.", "Texan Incorp."), Family_Type = c("N", 
"N", "O", "O", "O", "O"), Ship_Period = c("P1", "P2", "P1", "P2", 
"P1", "P2"), Q_S = c(1, 15, 4633, 57317, 251, 1205), B_S = c(1157, 
26958.4, 3736290.43, 6144393.02, 171699, 1022155.1), Q_A = c(1, 
2.14285714285714, 71.2769230769231, 707.617283950617, 25.1, 34.4285714285714
), B_A = c(1157, 3851.2, 57481.3912307692, 75856.7039506173, 
17169.9, 29204.4314285714), Q_M = c(1, 8, 1940, 11000, 234, 617
), B_M = c(1157, 17980, 1270354, 1463415.25, 128258, 341293.55
), n = c(1, 7, 65, 81, 10, 35)), .Names = c("Country_SL6", "Company.Name", 
"Family_Type", "Ship_Period", "Q_S", "B_S", "Q_A", "B_A", "Q_M", 
"B_M", "n"), row.names = c(NA, 6L), class = "data.frame")
Top_Act_Final<- Master_Final %>%
  #Spread out the columns so that we can do row-wise operations
  gather(key = Column, value = value,Q_S:n) %>%
  unite(P_Column, Ship_Period,Column,sep="_") %>%
  unite(F_P_Column, Family_Type,P_Column,sep="_") %>%
  spread(F_P_Column, value) %>%
  dplyr::rowwise(.)%>%
  #Notation:
  #N vs. O = New or Old
  #P1 vs. P2 = Period 1 vs. Period2
  #B vs. Q = Bookings vs. Quantity
  #S vs. A = Sum vs. Average

  dplyr::mutate(P12_B_S = sum(N_P1_B_S,O_P1_B_S,N_P2_B_S,O_P2_B_S,na.rm=TRUE),
                P12_Q_S = sum(N_P1_Q_S,O_P1_Q_S,N_P2_Q_S,O_P2_Q_S,na.rm=TRUE),
                P12_B_A = sum(N_P1_B_A*N_P1_n,  O_P1_B_A*O_P1_n,  N_P2_B_A*N_P2_n,  O_P2_B_A*O_P2_n,  na.rm = TRUE)/sum(N_P1_n,O_P1_n,N_P2_n,O_P2_n,na.rm=TRUE),
                P12_Q_A = sum(N_P1_B_A*N_P1_n,  O_P1_B_A*O_P1_n,  N_P2_B_A*N_P2_n,  O_P2_B_A*O_P2_n,  na.rm = TRUE)/sum(N_P1_n,O_P1_n,N_P2_n,O_P2_n,na.rm=TRUE),

                P2_B_A = sum(N_P2_B_A*N_P2_n,  O_P2_B_A*O_P2_n,  na.rm = TRUE)/sum(N_P2_n, O_P2_n,na.rm=TRUE),
                P2_Q_A = sum( N_P2_Q_A*N_P2_n,  O_P2_Q_A*O_P2_n,  na.rm = TRUE)/sum(N_P2_n, O_P2_n,na.rm=TRUE),
                P1_B_A = sum(N_P1_B_A*N_P1_n,  O_P1_B_A*O_P1_n,  na.rm = TRUE)/sum(N_P1_n, O_P1_n,na.rm=TRUE),
                P1_Q_A = sum( N_P1_Q_A*N_P1_n,  O_P1_Q_A*O_P1_n,  na.rm = TRUE)/sum(N_P1_n, O_P1_n,na.rm=TRUE),
                P1_B_S = sum(N_P1_B_S,  O_P1_B_S,  na.rm = TRUE),                  
                P1_Q_S = sum(N_P1_Q_S,  O_P1_Q_S,  na.rm = TRUE),                  

                P2_B_S = sum(N_P2_B_S,  O_P2_B_S,  na.rm = TRUE),                  
                P2_Q_S = sum(N_P2_Q_S,  O_P2_Q_S,  na.rm = TRUE),      

                P2_B_M = max(N_P2_B_M,O_P2_B_M, na.rm = TRUE),
                P2_Q_M = max(N_P2_Q_M,O_P2_Q_M, na.rm = TRUE),
                P1_n = sum(N_P1_n,O_P1_n,na.rm = TRUE),
                P2_n = sum(N_P2_n,O_P2_n,na.rm = TRUE))

#replace NaN or -Inf with NA
Top_Act_Final[is.nan(Top_Act_Final$P2_B_A),"P2_B_A"]<-NA
Top_Act_Final[is.nan(Top_Act_Final$P2_Q_A),"P2_Q_A"]<-NA
Top_Act_Final[is.nan(Top_Act_Final$P2_B_M),"P2_B_M"]<-NA
Top_Act_Final[is.nan(Top_Act_Final$P2_Q_M),"P2_Q_M"]<-NA

Top_Act_Final<-     Top_Act_Final %>%

  dplyr::mutate(P12_B_A =sum(P1_B_A*P1_n,P2_B_A*P2_n,na.rm=TRUE)/(P1_n+P2_n),
                P12_Q_A =sum(P1_Q_A*P1_n,P2_Q_A*P2_n,na.rm=TRUE)/(P1_n+P2_n),
                P12_B_S =sum(P1_B_S,P2_B_S,na.rm=TRUE),
                P12_Q_S =sum(P2_Q_S,P1_Q_S,na.rm=TRUE)) %>%

  dplyr::mutate(B_A_c = P2_B_A>P12_B_A,
                Q_A_c = P2_Q_A>P12_Q_A,
                B_M_c = P2_B_M>P12_B_A,
                Q_M_c = P2_Q_M>P12_Q_A,
                N_O_A_c = N_P2_B_A>O_P2_B_A,
                N_O_S_c = N_P2_B_S>O_P2_B_S) %>%

  dplyr::mutate(Comparison_p1_p2 = sum(B_A_c, Q_A_c, B_M_c, Q_M_c, na.rm = TRUE),
                Comparison_p2new_P2old = sum(N_O_A_c,N_O_S_c)) %>%

  dplyr::select(Country_SL6, Company.Name, Comparison_p1_p2, Comparison_p2new_P2old)
样本输出: 或者,您可以运行下面的代码来生成示例输出

dput(Top_Act_Final)
structure(list(Country_SL6 = c("United Kingdom", "United States"
), Company.Name = c("Texan Incorp.", "Mass Incorp."), Comparison_p1_p2 = c(4L, 
4L), Comparison_p2new_P2old = c(NA, 0L)), class = c("rowwise_df", 
"tbl_df", "tbl", "data.frame"), row.names = c(NA, -2L), .Names = c("Country_SL6", 
"Company.Name", "Comparison_p1_p2", "Comparison_p2new_P2old"))
代码:

N and O = New and Old respectively
P1 and P2 = Period 1 and Period2 respectively
B and Q = Bookings and Quantity respectively
S and A = Sum and Average respectively.
P1 = Period 1
P2 = Period 2
P12 = Period 1 + Period 2 = Total Period
n = count 
M = Max
dput(Master_Final)
structure(list(Country_SL6 = c("United States", "United States", 
"United States", "United States", "United Kingdom", "United Kingdom"
), Company.Name = c("Mass Incorp.", "Mass Incorp.", "Mass Incorp.", 
"Mass Incorp.", "Texan Incorp.", "Texan Incorp."), Family_Type = c("N", 
"N", "O", "O", "O", "O"), Ship_Period = c("P1", "P2", "P1", "P2", 
"P1", "P2"), Q_S = c(1, 15, 4633, 57317, 251, 1205), B_S = c(1157, 
26958.4, 3736290.43, 6144393.02, 171699, 1022155.1), Q_A = c(1, 
2.14285714285714, 71.2769230769231, 707.617283950617, 25.1, 34.4285714285714
), B_A = c(1157, 3851.2, 57481.3912307692, 75856.7039506173, 
17169.9, 29204.4314285714), Q_M = c(1, 8, 1940, 11000, 234, 617
), B_M = c(1157, 17980, 1270354, 1463415.25, 128258, 341293.55
), n = c(1, 7, 65, 81, 10, 35)), .Names = c("Country_SL6", "Company.Name", 
"Family_Type", "Ship_Period", "Q_S", "B_S", "Q_A", "B_A", "Q_M", 
"B_M", "n"), row.names = c(NA, 6L), class = "data.frame")
Top_Act_Final<- Master_Final %>%
  #Spread out the columns so that we can do row-wise operations
  gather(key = Column, value = value,Q_S:n) %>%
  unite(P_Column, Ship_Period,Column,sep="_") %>%
  unite(F_P_Column, Family_Type,P_Column,sep="_") %>%
  spread(F_P_Column, value) %>%
  dplyr::rowwise(.)%>%
  #Notation:
  #N vs. O = New or Old
  #P1 vs. P2 = Period 1 vs. Period2
  #B vs. Q = Bookings vs. Quantity
  #S vs. A = Sum vs. Average

  dplyr::mutate(P12_B_S = sum(N_P1_B_S,O_P1_B_S,N_P2_B_S,O_P2_B_S,na.rm=TRUE),
                P12_Q_S = sum(N_P1_Q_S,O_P1_Q_S,N_P2_Q_S,O_P2_Q_S,na.rm=TRUE),
                P12_B_A = sum(N_P1_B_A*N_P1_n,  O_P1_B_A*O_P1_n,  N_P2_B_A*N_P2_n,  O_P2_B_A*O_P2_n,  na.rm = TRUE)/sum(N_P1_n,O_P1_n,N_P2_n,O_P2_n,na.rm=TRUE),
                P12_Q_A = sum(N_P1_B_A*N_P1_n,  O_P1_B_A*O_P1_n,  N_P2_B_A*N_P2_n,  O_P2_B_A*O_P2_n,  na.rm = TRUE)/sum(N_P1_n,O_P1_n,N_P2_n,O_P2_n,na.rm=TRUE),

                P2_B_A = sum(N_P2_B_A*N_P2_n,  O_P2_B_A*O_P2_n,  na.rm = TRUE)/sum(N_P2_n, O_P2_n,na.rm=TRUE),
                P2_Q_A = sum( N_P2_Q_A*N_P2_n,  O_P2_Q_A*O_P2_n,  na.rm = TRUE)/sum(N_P2_n, O_P2_n,na.rm=TRUE),
                P1_B_A = sum(N_P1_B_A*N_P1_n,  O_P1_B_A*O_P1_n,  na.rm = TRUE)/sum(N_P1_n, O_P1_n,na.rm=TRUE),
                P1_Q_A = sum( N_P1_Q_A*N_P1_n,  O_P1_Q_A*O_P1_n,  na.rm = TRUE)/sum(N_P1_n, O_P1_n,na.rm=TRUE),
                P1_B_S = sum(N_P1_B_S,  O_P1_B_S,  na.rm = TRUE),                  
                P1_Q_S = sum(N_P1_Q_S,  O_P1_Q_S,  na.rm = TRUE),                  

                P2_B_S = sum(N_P2_B_S,  O_P2_B_S,  na.rm = TRUE),                  
                P2_Q_S = sum(N_P2_Q_S,  O_P2_Q_S,  na.rm = TRUE),      

                P2_B_M = max(N_P2_B_M,O_P2_B_M, na.rm = TRUE),
                P2_Q_M = max(N_P2_Q_M,O_P2_Q_M, na.rm = TRUE),
                P1_n = sum(N_P1_n,O_P1_n,na.rm = TRUE),
                P2_n = sum(N_P2_n,O_P2_n,na.rm = TRUE))

#replace NaN or -Inf with NA
Top_Act_Final[is.nan(Top_Act_Final$P2_B_A),"P2_B_A"]<-NA
Top_Act_Final[is.nan(Top_Act_Final$P2_Q_A),"P2_Q_A"]<-NA
Top_Act_Final[is.nan(Top_Act_Final$P2_B_M),"P2_B_M"]<-NA
Top_Act_Final[is.nan(Top_Act_Final$P2_Q_M),"P2_Q_M"]<-NA

Top_Act_Final<-     Top_Act_Final %>%

  dplyr::mutate(P12_B_A =sum(P1_B_A*P1_n,P2_B_A*P2_n,na.rm=TRUE)/(P1_n+P2_n),
                P12_Q_A =sum(P1_Q_A*P1_n,P2_Q_A*P2_n,na.rm=TRUE)/(P1_n+P2_n),
                P12_B_S =sum(P1_B_S,P2_B_S,na.rm=TRUE),
                P12_Q_S =sum(P2_Q_S,P1_Q_S,na.rm=TRUE)) %>%

  dplyr::mutate(B_A_c = P2_B_A>P12_B_A,
                Q_A_c = P2_Q_A>P12_Q_A,
                B_M_c = P2_B_M>P12_B_A,
                Q_M_c = P2_Q_M>P12_Q_A,
                N_O_A_c = N_P2_B_A>O_P2_B_A,
                N_O_S_c = N_P2_B_S>O_P2_B_S) %>%

  dplyr::mutate(Comparison_p1_p2 = sum(B_A_c, Q_A_c, B_M_c, Q_M_c, na.rm = TRUE),
                Comparison_p2new_P2old = sum(N_O_A_c,N_O_S_c)) %>%

  dplyr::select(Country_SL6, Company.Name, Comparison_p1_p2, Comparison_p2new_P2old)
Top\u Act\u Final%
#将列展开,以便我们可以执行行操作
聚集(键=列,值=值,Q_S:n)%>%
联合(P_列、装运期、列、sep=“”)%>%
unite(F_P_列,族类型,P_列,sep=“”)%>%
排列(F_P_列,值)%>%
dplyr::按行(%)%%>%
#符号:
#N与O=新的或旧的
#P1与P2=周期1与周期2
#B与Q=预订与数量
#S与A=总和与平均值
dplyr::mutate(P12_B_S=sum(N_P1_B_S,O_P1_B_S,N_P2_B_S,O_P2_B_S,na.rm=TRUE),
P12_Q_S=总和(N_P1_Q_S,O_P1_Q_S,N_P2_Q_S,O_P2_Q_S,na.rm=真),
P12_B_A=和(N_P1_B_A*N_P1_N,O_P1_A*O_P1_N,N_P2_A*N_P2_N,O_P2_A*O_P2_N,na.rm=真)/和(N_P1_N,O_P1_N,N,N_P2_N,O_P2_N,O_N,na.rm=真),
P12_Q_A=和(N_P1_B_A*N_P1_N,O_P1_A*O_P1_N,N_P2_B_A*N_P2_N,O_P2_A*O_P2_N,na.rm=真)/和(N_P1_N,O_P1_N,N,N_P2_N,O_P2_N,O_N,na.rm=真),
P2_B_A=和(N_P2_B_A*N_P2_N,O_P2_A*O_P2_N,na.rm=TRUE)/和(N_P2_N,O_P2_N,na.rm=TRUE),
P2_Q_A=sum(N_P2_Q_A*N_P2_N,O_P2_A*O_P2_N,na.rm=TRUE)/sum(N_P2_N,O_P2_N,na.rm=TRUE),
P1_B_A=总和(N_P1_B_A*N_P1_N,O_P1_A*O_P1_N,na.rm=TRUE)/总和(N_P1_N,O_P1_N,na.rm=TRUE),
P1_Q_A=和(N_P1_Q_A*N_P1_N,O_P1_A*O_P1_N,na.rm=TRUE)/sum(N_P1_N,O_P1_N,na.rm=TRUE),
P1_B_S=总和(N_P1_B_S,O_P1_B_S,na.rm=真),
P1_Q_S=总和(N_P1_Q_S,O_P1_Q_S,na.rm=真),
P2_B_S=总和(N_P2_B_S,O_P2_B_S,na.rm=真),
P2_Q_S=总和(N_P2_Q_S,O_P2_Q_S,na.rm=真),
P2_B_M=最大值(N_P2_B_M,O_P2_B_M,na.rm=真),
P2_Q_M=max(N_P2_Q_M,O_P2_Q_M,na.rm=TRUE),
P1_n=总和(n_P1_n,O_P1_n,na.rm=真),
P2_n=总和(n_P2_n,O_P2_n,na.rm=真))
#用NA替换NaN或-Inf
顶级演技决赛[is.nan(顶级演技决赛$P2_B_A),“P2_B_A”]P12_B_A,
Q_M_c=P2_Q_M>P12_Q_A,
N_O_A_c=N_P2_B_A>O_P2_B_A,
N_O_S_c=N_P2_B_S>O_P2_B_S)%>%
dplyr::mutate(比较p1\p2=sum(B_A_c,Q_A_c,B_M_c,Q_M_c,na.rm=TRUE),
比较\u p2new\u P2old=总和(N\u O\u A\u c,N\u O\u S\u c))%>%
dplyr::选择(国家/地区、公司名称、比较\u p1\u p2、比较\u p2new\u P2old)
很抱歉发了这么长的帖子,但我想尽量详细。我们是否可以缩短我的代码


我上周写了这段代码,今天在复习(重新使用)时,我经历了一段艰难的时光。我非常感谢您的帮助。

我不确定我是否完全遵守了,但您的代码是否有可能通过将数据保持为“长”格式而变得更简单?例如,使用类似于
[国家、公司、家庭(新/旧)、时期(P1/P2)、类型(数量/预订)、统计(总和/平均数/最大值),…]
@Alexis_laz-谢谢,但您介意使用长格式发布解决方案吗?归根结底,格式并不重要——任何让我的代码更简单的东西都是最好的。此外,我已经在上面发布了完整的工作解决方案。另外,您还没有真正指定不清楚的内容。我很感激你的想法。我从这一点开始,但不清楚你的输出列是什么——你列出了四个要计算的比较,但不知怎的归结为两个结果列,它们都没有映射到你请求的列。@Mark-谢谢你的帮助。输出列为
Country\u SL6
Company.Name
Comparison\u p1\u p2
Comparison\u p2new\u p2old
。最后第二列是通过行求和生成的