按列表对 Dataframe 中的列进行分组,并在R中进行变异

bq3bfh9z  于 2023-02-10  发布在  其他
关注(0)|答案(1)|浏览(98)

我有一个包含二进制列的大型 Dataframe 。下面是列名称的列表:

[1] "imagetag_logos_position_Apple_BOTTOM_CENTER"         "imagetag_logos_position_Apple_BOTTOM_LEFT"           "imagetag_logos_position_Apple_BOTTOM_RIGHT"          "imagetag_logos_position_Apple_CENTER"                "imagetag_logos_position_Apple_CENTER_LEFT"          
  [6] "imagetag_logos_position_Apple_CENTER_RIGHT"          "imagetag_logos_position_Apple_TOP_CENTER"            "imagetag_logos_position_Apple_TOP_LEFT"              "imagetag_logos_position_Apple_TOP_RIGHT"             "imagetag_logos_position_Banana_BOTTOM_CENTER"       
 [11] "imagetag_logos_position_Banana_BOTTOM_LEFT"          "imagetag_logos_position_Banana_BOTTOM_RIGHT"         "imagetag_logos_position_Banana_CENTER_LEFT"          "imagetag_logos_position_Banana_CENTER_RIGHT"         "imagetag_logos_position_Banana_TOP_RIGHT"           
 [16] "imagetag_logos_position_Pear_BOTTOM_CENTER"      "imagetag_logos_position_Pear_BOTTOM_LEFT"        "imagetag_logos_position_Pear_BOTTOM_RIGHT"       "imagetag_logos_position_Pear_CENTER"             "imagetag_logos_position_Pear_CENTER_LEFT"       
 [21] "imagetag_logos_position_Pear_CENTER_RIGHT"       "imagetag_logos_position_Pear_TOP_RIGHT"          "imagetag_logos_position_Kiwi_BOTTOM_CENTER"    "imagetag_logos_position_Kiwi_BOTTOM_LEFT"      "imagetag_logos_position_Kiwi_BOTTOM_RIGHT"    
 [26] "imagetag_logos_position_Kiwi_CENTER_LEFT"      "imagetag_logos_position_Kiwi_CENTER_RIGHT"     "imagetag_logos_position_Kiwi_TOP_LEFT"         "Product_position_Product_0"                         "Product_position_Product_BOTTOM_CENTER"            
 [31] "Product_position_Product_BOTTOM_LEFT"               "Product_position_Product_BOTTOM_RIGHT"              "Product_position_Product_CENTER"                    "Product_position_Product_CENTER_LEFT"               "Product_position_Product_CENTER_RIGHT"             
 [36] "Product_position_Product_TOP_CENTER"                "Product_position_Product_TOP_LEFT"                  "Product_position_Product_TOP_RIGHT"                 "Person_position_Person_0"                           "Person_position_Person_BOTTOM_CENTER"              
 [41] "Person_position_Person_BOTTOM_LEFT"                 "Person_position_Person_BOTTOM_RIGHT"                "Person_position_Person_CENTER"                      "Person_position_Person_CENTER_LEFT"                 "Person_position_Person_CENTER_RIGHT"               
 [46] "Person_position_Person_TOP_CENTER"                  "Person_position_Person_TOP_LEFT"                    "Person_position_Person_TOP_RIGHT"                   "Logo_position_Logo_0"                               "Logo_position_Logo_BOTTOM_CENTER"                  
 [51] "Logo_position_Logo_BOTTOM_LEFT"                     "Logo_position_Logo_BOTTOM_RIGHT"                    "Logo_position_Logo_CENTER"                          "Logo_position_Logo_CENTER_LEFT"                     "Logo_position_Logo_CENTER_RIGHT"                   
 [56] "Logo_position_Logo_TOP_CENTER"                      "Logo_position_Logo_TOP_LEFT"                        "Logo_position_Logo_TOP_RIGHT"                       "CTA_ShopNow_position_Shop Now_0"                    "CTA_ShopNow_position_Shop Now_BOTTOM_CENTER"       
 [61] "CTA_ShopNow_position_Shop Now_BOTTOM_LEFT"          "CTA_ShopNow_position_Shop Now_BOTTOM_RIGHT"         "CTA_ShopNow_position_Shop Now_CENTER"               "CTA_ShopNow_position_Shop Now_CENTER_LEFT"          "CTA_ShopNow_position_Shop Now_CENTER_RIGHT"        
 [66] "CTA_ShopNow_position_Shop Now_TOP_CENTER"           "CTA_ShopNow_position_Shop Now_TOP_RIGHT"            "CTA_JoinNow_position_Join Now_0"                    "CTA_JoinNow_position_Join Now_BOTTOM_CENTER"        "CTA_JoinNow_position_Join Now_BOTTOM_LEFT"         
 [71] "CTA_JoinNow_position_Join Now_BOTTOM_RIGHT"         "CTA_JoinNow_position_Join Now_CENTER"               "CTA_JoinNow_position_Join Now_CENTER_RIGHT"         "CTA_JoinNow_position_Join Now_TOP_CENTER"           "CTA_JoinNow_position_Join Now_TOP_RIGHT"           
 [76] "CTA_position_CTA_0"                                 "CTA_position_CTA_BOTTOM_CENTER"                     "CTA_position_CTA_BOTTOM_LEFT"                       "CTA_position_CTA_BOTTOM_RIGHT"                      "CTA_position_CTA_CENTER"                           
 [81] "CTA_position_CTA_CENTER_LEFT"                       "CTA_position_CTA_CENTER_RIGHT"                      "CTA_position_CTA_TOP_CENTER"                        "CTA_position_CTA_TOP_LEFT"                          "CTA_position_CTA_TOP_RIGHT"                        
 [86] "Text_position_text_BOTTOM_CENTER"                   "Text_position_text_BOTTOM_LEFT"                     "Text_position_text_BOTTOM_RIGHT"                    "Text_position_text_CENTER"                          "Text_position_text_CENTER_LEFT"                    
 [91] "Text_position_text_CENTER_RIGHT"                    "Text_position_text_TOP_CENTER"                      "Text_position_text_TOP_LEFT"                        "Text_position_text_TOP_RIGHT"                       "Product_position_Product_0_LF"                     
 [96] "Product_position_Product_BOTTOM_CENTER_LF"          "Product_position_Product_BOTTOM_LEFT_LF"            "Product_position_Product_BOTTOM_RIGHT_LF"           "Product_position_Product_CENTER_LF"                 "Product_position_Product_CENTER_LEFT_LF"           
[101] "Product_position_Product_CENTER_RIGHT_LF"           "Product_position_Product_TOP_CENTER_LF"             "Product_position_Product_TOP_LEFT_LF"               "Product_position_Product_TOP_RIGHT_LF"              "Logo_position_Logo_0_LF"

我想对其中一些列进行分组,例如对包含"BOTTOM_CENTER"、"BOTTOM_RIGHT"、"BOTTOM_LEFT"的列求和。但是,我需要在每个匹配的前缀内对它们进行分组,例如,仅对imagetag_logos_position_Apple求和,并对imagetag_logos_position_Banana单独求和。
我这样做是为了创建一个唯一前缀列表:
x一个一个一个一个x一个一个二个x
我已经尝试了不同的方法来让 Dataframe 按列表中的字符串分组,以便我可以执行列的添加,但似乎无法弄清楚如何去做。% in %将不支持部分匹配,所以我不知道使用其他函数谢谢!

for(i in prefix_list1){
  sapply(positionsdf, function(x) i %in% x)
}
fd3cxomn

fd3cxomn1#

我们可以

sapply(prefix_list1, function(pat) {
        nm1 <- grep(pat, names(positions_df), value = TRUE)
        nm2 <- grep("BOTTOM_(CENTER|RIGHT|LEFT)", nm1, value = TRUE)
        rowSums(positions_df[nm2], na.rm = TRUE)
    })

相关问题