首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >R数据帧列到列表(矩阵行名称)

R数据帧列到列表(矩阵行名称)
EN

Stack Overflow用户
提问于 2015-01-28 14:41:52
回答 1查看 901关注 0票数 0

我需要根据数据帧列中的值更新我的矩阵行名。我知道我可以这样更新我的矩阵行名称:

代码语言:javascript
复制
dimnames(A) = list( 
  +   c("row1", "row2"),         # row names 
  +   c("col1", "col2", "col3")) # column names 

然而,名字"row1“,”row2“.应该来自具有以下值的数据帧列的值:

代码语言:javascript
复制
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"   

我的问题实际上是如何提取这个数据帧列的值,因此它的形式是:

代码语言:javascript
复制
"Samsung UN48H6350 48\"" , "Sharp LC-60LE650U 60"  , "Roku Streaming Stick with remote"  ... 

因此,我可以把这个列表作为参数传递给我尝试过的名称(A):

代码语言:javascript
复制
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")
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-01-28 14:50:25

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

代码语言:javascript
复制
49 iPads, Tablets & eBook Readers     171485             90210         0               
51 iPads, Tablets & eBook Readers     171485             90210        NA           <NA>

如果你想要解决这个问题,就不应该出现。一旦您这样做,您最初的rownames(products) <- products[,2]尝试将运行良好。

如果必须按原样使用行名,则矩阵允许重复名称:

代码语言:javascript
复制
> 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    
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28195016

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档