R中是否有特定功能来合并2个向量

问题描述

我有两个向量,一个包含一个变量列表,另一个包含日期,例如

Variables_Pays <- c("PIB","ConsommationPrivee","ConsommationPubliques","FBCF","ProductionIndustrielle","Inflation","InflationSousJacente","PrixProductionIndustrielle","CoutHoraireTravail")
Annee_Pays <- c("2000","2001")

我想将它们合并为一个向量,并且每个变量都按我的日期索引,这就是我想要的输出

> Colonnes_Pays_Principaux
 [1] "PIB_2020"                        "PIB_2021"                        "ConsommationPrivee_2020"        
 [4] "ConsommationPrivee_2021"         "ConsommationPubliques_2020"      "ConsommationPubliques_2021"     
 [7] "FBCF_2020"                       "FBCF_2021"                       "ProductionIndustrielle_2020"    
[10] "ProductionIndustrielle_2021"     "Inflation_2020"                  "Inflation_2021"                 
[13] "InflationSousJacente_2020"       "InflationSousJacente_2021"       "PrixProductionIndustrielle_2020"
[16] "PrixProductionIndustrielle_2021" "CoutHoraireTravail_2020"         "CoutHoraireTravail_2021" 

有没有比我在下面尝试过并成功的双for循环更简单/更易读的方式?

Colonnes_Pays_Principaux <- vector()
for (Variable in (1:length(Variables_Pays))){
  for (Annee in (1:length(Annee_Pays))){
     Colonnes_Pays_Principaux=
       append(Colonnes_Pays_Principaux,paste(Variables_Pays[Variable],Annee_Pays[Annee],sep="_")
              )
  }
}

解决方法

expand.grid将使用两个向量的所有组合创建一个数据帧。

with(
  expand.grid(Variables_Pays,Annee_Pays),paste0(Var1,"_",Var2)
)
#>  [1] "PIB_2000"                        "ConsommationPrivee_2000"        
#>  [3] "ConsommationPubliques_2000"      "FBCF_2000"                      
#>  [5] "ProductionIndustrielle_2000"     "Inflation_2000"                 
#>  [7] "InflationSousJacente_2000"       "PrixProductionIndustrielle_2000"
#>  [9] "CoutHoraireTravail_2000"         "PIB_2001"                       
#> [11] "ConsommationPrivee_2001"         "ConsommationPubliques_2001"     
#> [13] "FBCF_2001"                       "ProductionIndustrielle_2001"    
#> [15] "Inflation_2001"                  "InflationSousJacente_2001"      
#> [17] "PrixProductionIndustrielle_2001" "CoutHoraireTravail_2001" 
,

我们可以使用outer

c(t(outer(Variables_Pays,Annee_Pays,paste,sep = '_')))

# [1] "PIB_2000"                        "PIB_2001"                       
# [3] "ConsommationPrivee_2000"         "ConsommationPrivee_2001"        
# [5] "ConsommationPubliques_2000"      "ConsommationPubliques_2001"     
# [7] "FBCF_2000"                       "FBCF_2001"                      
# [9] "ProductionIndustrielle_2000"     "ProductionIndustrielle_2001"    
#[11] "Inflation_2000"                  "Inflation_2001"                 
#[13] "InflationSousJacente_2000"       "InflationSousJacente_2001"      
#[15] "PrixProductionIndustrielle_2000" "PrixProductionIndustrielle_2001"
#[17] "CoutHoraireTravail_2000"         "CoutHoraireTravail_2001" 
,

没有真正的需要超越这里的基础知识!使用paste粘贴字符串,然后使用rep重复Annee_PaysVariables_Pays以获得所有组合:

Variables_Pays <- c("PIB","ConsommationPrivee","ConsommationPubliques","FBCF","ProductionIndustrielle","Inflation","InflationSousJacente","PrixProductionIndustrielle","CoutHoraireTravail")
Annee_Pays <- c("2000","2001")

# To get this is the same order as in your example:
paste(rep(Variables_Pays,rep(2,length(Variables_Pays))),sep = "_")

# Alternative order:
paste(Variables_Pays,rep(Annee_Pays,rep(length(Variables_Pays),2)),sep = "_")

# Or,if order doesn't matter too much:
paste(Variables_Pays,length(Variables_Pays)),sep = "_")
,

在基数R中:

Variables_Pays <- c("PIB","2001")
cbind(paste(Variables_Pays,sep="_"),paste(Variables_Pays,rev(Annee_Pays),sep="_")