要列出的R数据框列(矩阵行名称)

时间:2015-01-28 14:41:53

标签: r list matrix dataframe

我需要根据来自数据框列的值更新矩阵行名称。我知道我可以更新我的矩阵行名称:

dimnames(A) = list( 
  +   c("row1", "row2"),         # row names 
  +   c("col1", "col2", "col3")) # column names 

然而,名称" row1" ," row2" ...应来自具有以下值的数据框列的值:

products[,2]
 [1] "Samsung UN48H6350 48\""                                                       "Sharp LC-60LE650U 60"                                                        
 [3] "Roku Streaming Stick with remote"                                             "Samsung Galaxy Tab Pro 8.4 16GB, Wi-Fi, 8.4in - White"                       
 [5] "GoPro Here 3 silver"                                                          "Brunswick T Zone 16 lb. bowling ball"                                        
 [7] "SDBS-150 weight set"                                                          "Aventus by Creed Eau De Parfum Spray 4 oz Men"                               
 [9] "Sunny SF-E906"                                                                "Sabrent 4-Port USB 2.0 Hub with Individual Power Switches and LEDs (HB-UMLS)"
[11] "Magnasonic MAG-MM176K"                                                        "NETGEAR Nighthawk AC1900  (R7000)"                                           
[13] "FIDO U2F Security Key"                                                        "Foscam FI9821W V2 white"                                                     
[15] "Samsung UN22F5000"                                                            "D&K Seamless Full Length Leggings"                                           
[17] "Taste of the Wild Dry Dog Food, Hi Prairie Canine Formula with Roasted Bison" "Tidy Cats 4 Count Breeze Litter Pad Refill (Pack of 10)"                     
[19] "PetSafe 6-Volt Lithium Battery (2 Batteries per Pack)"                        "Hakko CHP-170 Micro Clean Cutter"                                            
[21] "Farm Innovators CC-2 Cord Connect Water-Tight Cord Lock - Green"              "Komelon SL2925 Self Lock Speed Mark"                                         
[23] "SanDisk 32GB Ultra Class 10 Micro SDHC"                                       "Samsung (MB-MP32DA/AM)"                                                      
[25] "Panasonic ER-GN30-K"                                                          "Ubervita W700"                                                               
[27] "Baby Einstein Take Along Tunes"                                               "Nosefrida The Snotsucker Nasal Aspirator"                                    
[29] "Baby Einstein Bendy Ball"                                                     "Mommy's Helper Outlet Plugs 36 Pack"                                         
[31] "Marcy Recumbent Exercise Bike: ME-709"                                        "Doc McStuffins Get Better Talking Mobile Cart"                               
[33] "Call of Duty: Advanced Warfare Day Zero Edition (PS3)"                        "VIZIO E320i-B2"                                                              
[35] "Peg Perego John Deere Gator XUV"                                              "KidKraft Uptown Play Kitchen, Espresso"                                      
[37] "Lifetime 44\" Pro Court Height-Adjustable Portable Basketball Hoop"           "Canon PIXMA MG2920 printer"                                                  
[39] "Roadmaster Granite Peak 26\" Men's Mountain Bike, Black"                      "Zoomer Zuppies Interactive Puppy, Roxy"                                      
[41] "Butterball Digital Electric Extra-Large Fryer"                                "Mongoose Dolomite Mens 7-speed"                                              
[43] "LEGO Cuusoo Minecraft Building Set"                                           "Hoover SteamVac F5914900"                                                    
[45] "Razor RipRider 360 Ride-On, Blue"                                             "Cosco - Scenera Convertible Car Seat Renaissance"                            
[47] "Brother PE770"                                                                "RCA RTD317W"                                                                 
[49] "iPads, Tablets & eBook Readers"                                               "Televisions"                                                                 
[51] "iPads, Tablets & eBook Readers"                                               "Vacuum Cleaners"   

我的问题实际上是如何提取此数据框列的值,因此它将采用以下形式:

"Samsung UN48H6350 48\"" , "Sharp LC-60LE650U 60"  , "Roku Streaming Stick with remote"  ... 

所以我可以把这个列表作为参数传递给dimnames(A) 我试过了:

rownames(products) <- products[,2] 
Error in rownames<-(*tmp*, value = c("Samsung UN48H6350 48\"", "Sharp LC-60LE650U 60", : length of 'dimnames' [1] not equal to array extent

> dput(products)
structure(list(id = 1:53, keywords = c("Samsung UN48H6350 48\"", 
"Sharp LC-60LE650U 60", "Roku Streaming Stick with remote", "Samsung Galaxy Tab Pro 8.4 16GB, Wi-Fi, 8.4in - White", 
"GoPro Here 3 silver", "Brunswick T Zone 16 lb. bowling ball", 
"SDBS-150 weight set", "Aventus by Creed Eau De Parfum Spray 4 oz Men", 
"Sunny SF-E906", "Sabrent 4-Port USB 2.0 Hub with Individual Power Switches and LEDs (HB-UMLS)", 
"Magnasonic MAG-MM176K", "NETGEAR Nighthawk AC1900  (R7000)", 
"FIDO U2F Security Key", "Foscam FI9821W V2 white", "Samsung UN22F5000", 
"D&K Seamless Full Length Leggings", "Taste of the Wild Dry Dog Food, Hi Prairie Canine Formula with Roasted Bison", 
"Tidy Cats 4 Count Breeze Litter Pad Refill (Pack of 10)", "PetSafe 6-Volt Lithium Battery (2 Batteries per Pack)", 
"Hakko CHP-170 Micro Clean Cutter", "Farm Innovators CC-2 Cord Connect Water-Tight Cord Lock - Green", 
"Komelon SL2925 Self Lock Speed Mark", "SanDisk 32GB Ultra Class 10 Micro SDHC", 
"Samsung (MB-MP32DA/AM)", "Panasonic ER-GN30-K", "Ubervita W700", 
"Baby Einstein Take Along Tunes", "Nosefrida The Snotsucker Nasal Aspirator", 
"Baby Einstein Bendy Ball", "Mommy's Helper Outlet Plugs 36 Pack", 
"Marcy Recumbent Exercise Bike: ME-709", "Doc McStuffins Get Better Talking Mobile Cart", 
"Call of Duty: Advanced Warfare Day Zero Edition (PS3)", "VIZIO E320i-B2", 
"Peg Perego John Deere Gator XUV", "KidKraft Uptown Play Kitchen, Espresso", 
"Lifetime 44\" Pro Court Height-Adjustable Portable Basketball Hoop", 
"Canon PIXMA MG2920 printer", "Roadmaster Granite Peak 26\" Men's Mountain Bike, Black", 
"Zoomer Zuppies Interactive Puppy, Roxy", "Butterball Digital Electric Extra-Large Fryer", 
"Mongoose Dolomite Mens 7-speed", "LEGO Cuusoo Minecraft Building Set", 
"Hoover SteamVac F5914900", "Razor RipRider 360 Ride-On, Blue", 
"Cosco - Scenera Convertible Car Seat Renaissance", "Brother PE770", 
"RCA RTD317W", "iPads, Tablets & eBook Readers", "Televisions", 
"iPads, Tablets & eBook Readers", "Vacuum Cleaners", "Bicycless"
), categories = c("11071", "11071", "168058", "171485", "625", 
"36105", "888", "26396", "15273", NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
"171485", "11071", "171485", "20614", "20614"), buyer_postal_code = c("90210", 
"90210", "90210", "90210", "90210", "90210", "90210", "90210", 
"90210", "90210", "90210", "90210", "90210", "90210", "90210", 
"90210", "90210", "90210", "90210", "90210", "90210", "90210", 
"90210", "90210", "90210", "90210", "90210", "90210", "90210", 
"90210", "90210", "90210", "90210", "90210", "90210", "90210", 
"90210", "90210", "90210", "90210", "90210", "90210", "90210", 
"90210", "90210", "90210", "90210", "90210", "90210", "90210", 
"90210", "90210", "90210"), min_price = c(200, 200, 0, 0, 100, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 100, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, NA, NA, NA, NA), seller_item_id = c(NA, NA, NA, NA, 
NA, NA, "111384136042", "111423002580", "191217437226", NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, "", NA, NA, NA, NA)), .Names = c("id", "keywords", 
"categories", "buyer_postal_code", "min_price", "seller_item_id"
), row.names = c(NA, -53L), class = "data.frame")

1 个答案:

答案 0 :(得分:0)

您必须首先解决重复记录的问题,因为不允许数据框具有重复的行名称。例如

49 iPads, Tablets & eBook Readers     171485             90210         0               
51 iPads, Tablets & eBook Readers     171485             90210        NA           <NA>

如果您想要解决此问题,则不应出现。一旦你这样做,你rownames(products) <- products[,2]的原始尝试就可以了。

如果您必须按原样使用rownames,那么矩阵 允许重复的名称:

> x <- as.matrix(products)
> rownames(x) <- x[, 2]
> head(x)                                                         
                                                      id   keywords                                                categories buyer_postal_code min_price seller_item_id
Samsung UN48H6350 48"                                 " 1" "Samsung UN48H6350 48\""                                "11071"    "90210"           "200"     NA            
Sharp LC-60LE650U 60                                  " 2" "Sharp LC-60LE650U 60"                                  "11071"    "90210"           "200"     NA            
Roku Streaming Stick with remote                      " 3" "Roku Streaming Stick with remote"                      "168058"   "90210"           "  0"     NA            
Samsung Galaxy Tab Pro 8.4 16GB, Wi-Fi, 8.4in - White " 4" "Samsung Galaxy Tab Pro 8.4 16GB, Wi-Fi, 8.4in - White" "171485"   "90210"           "  0"     NA            
GoPro Here 3 silver                                   " 5" "GoPro Here 3 silver"                                   "625"      "90210"           "100"     NA            
Brunswick T Zone 16 lb. bowling ball                  " 6" "Brunswick T Zone 16 lb. bowling ball"                  "36105"    "90210"           "  0"     NA