从数据框中删除空值

时间:2018-03-29 22:21:14

标签: r

library(rvest)

Holidays <- read_html("http://www.spholidays.com/monuments.php")

a <- data.frame(Places = html_text(html_nodes(Holidays,".cat-hd1")))

b <- data.frame(Monuments_Name = html_text(html_nodes(Holidays,"tr~ tr+ tr td:nth-child(1)")))

b <- as.data.frame(b)

b<-b[!apply(b == "", 1, all),]

b<-b[!apply(b == "", 1, all),]
  

申请出错(b ==&#34;&#34;,1,全部):dim(X)必须有正长度

    b  
  [1] Taj Mahal                                                                                                                        
  [3] Red Fort in Agra                                                                                                                 
  [5] Fatehpur Sikri                                                                                                                   
  [7] Itmad-Ud-Daula / Baby Taj                                                                                                        
  [9] Sikandara                                                                                                                        
 [11] Red Fort                                                                                                                         
 [13] Jama Mosque                                                                                                                      
 [15] Qutab Tower                                                                                                                      
 [17] Lotus Temple                                                                                                                     
 [19] Jantar Mantar                                                                                                                    
 [21] Akshardham Temple                                                                                                                
 [23] Birla Temple                                                                                                                     
 [25] Raj Ghat                                                                                                                         
 [27] India Gate                                                                                                                       
 [29] President house & Mughal Garden                                                                                                  
 [31] National Rail Museum                                                                                                             
 [33] Humayun's Tomb                                                                                                                   
 [35] Gandhi National Museum                                                                                                           
 [37] Chatarpur temple                                                                                                                 
 [39] National Museum                                                                                                                  
 [41] Gandhi Smriti                                                                                                                    
 [43] Amber Fort                                                                                                                       
 [45] Jaigarh Fort                                                                                                                     
 [47] Nahargarh Fort                                                                                                                   
 [49] Jalmahal Palace                                                                                                                  
 [51] Hawa Mahal or Wind Palace                                                                                                        
 [53] Jantar Mantar                                                                                                                    
 [55] City Palace                                                                                                                      
 [57] Chowki Dhani - An ethnic Village                                                                                                 
 [59] Elephant Ride at Amber Fort                                                                                                      
 [61] Mahrangarh Fort                                                                                                                  
 [63] Umaid Bhawan Palace                                                                                                              
 [65] Jaswant Thada                                                                                                                    
 [67] Government Museum                                                                                                                
 [69] Clock Tower and Sadar Market                                                                                                     
 [71] Mahamandir Temple                                                                                                                
 [73] City Palace                                                                                                                      
 [75] Lake Pichola                                                                                                                     
 [77] City Palace Museum                                                                                                               
 [79] Shiilpgram Museum                                                                                                                
 [81] Fateh Sagar Lake                                                                                                                 
 [83] Jaisamand Lake                                                                                                                   
 [85] Rajsamand Lake                                                                                                                   
 [87] Manson Palace                                                                                                                    
 [89] Nagda and Eklinjgi                                                                                                               
 [91] Ranakpur                                                        
 [93] Jagmandir Island                                                
 [95] Saheliyon ki bari                                               
 [97] Chttorgarh                                                      
 [99] Kumbhalgarh Fort                                                
[101] Junagarh Fort                                                   
[103] Lalgarh Palace                                                  
[105] Karni Mata Temple(Rats Temple)                                  
[107] Camel breeding farm house                                       
[109] Gajner Wildlife sanctury                                        
[111] Shiv bari temple                                                
[113] Jaisalmer Fort                                                  
[115] Nath Mal Ji ki haveli                                           
[117] Patwon ki haveli                                                
[119] Salim Singh ki Haveli                                           
[121] Desert national Park                                            
[123] Gadsisar lake                                                   
[125] Haveli Museum                                                   
[127] Jain Temples                                                    
[129] Sand Dunes                                                      
[131] Tazia Tower                                                     
[133] Taragarh Fort                                                   
[135] Adhai Din ka Jhonpara                                           
[137] Dargah of Garib Nawaj                                           
[139] Nasiyan Jain Temple                                             
[141] Brahma Temple                                                   
[143] Pushkar Lake                                                    
[145] Savitri Temple                                                  
[147] Mahadeva Temple                                                 
[149] Rangji Temple                                                   
[151] Varaha Temple                                                   
[153] Pushkar Fair                                                    
[155] Ranthambore fort                                                
[157] Ranthambore jungle safari :---                                  
[159] Park Safari by Gypsy Sharing Basis Per Pax                                                                                       
[161] Park Safari by Canter per ride Per Pax                          
[163]                                                                                                                                  
[165] Park Safari by Gypsy(Exclusive up to 04 Pax)\n\t\n\n                                                                              
[167] Choice Guide  For Jeep Safari                                   
[169] Choice Guide  For Canter Safari                                 
[171] Camel Ride  (Outside of the Park)                               
[173] For 02 pax for  01 Hour                                                                                                          
[175]                                                                                                                                  
[177] Benares Hindu University- Bharat Kala Museum                                                                                     
[179] Bharat Mata Temple                                                                                                               
[181] Durga Temple                                                                                                                     
[183] Nepali Temple(Kathwala Temple)                                                                                                   
[185] Vishwanath Temple(Golden Temple)                                                                                                 
[187] Tulsi Manas Temple                                                                                                               
[189] Ram Nagar Fort                                                                                                                   
[191] Sarnath                                                                                                                          
[193] Khajuraho Temples                                                                                                                
[195] Sound & Light Show at Khajuraho                                                                                                  
[197] Ajanta                                                                                                                           
[199] Ellora                                                                                                                           
[201] Mini Taj (Bibi ka Maqbara)                                                                                                       
[203] Daulatabad Fort                                                                                                                  
[205] Jews Synagogue                                                                                                                   
[207] St Francis church                                                                                                                
[209] Dutch Palace                                                                                                                     
[211] Madras Govt.Museum                                                                                                               
[213] Madras Fort Museum                                                                                                               
[215]                                                                                                                                  
[217] Eakambareshwara Temple                                                                                                           
[219] Kailasanatha temple                                                                                                              
[221] Varadharaja Temple                                                                                                               
[223] Tiruvannamalai Gingey                                                                                                            
[225] Pondicherry Museum                                                                                                               
[227] Tanjore Art Gallery                                                                                                              
[229] Srirangam Temple                                                                                                                 
[231] Rockfort                                                                                                                         
[233] Madurai Palace                                                                                                                   
[235] Madurai Temple                                                                                                                   
[237] Vivekananda Rock                                                                                                                 
[239] Mysore palace                                                                                                                    
[241] Srirangapatana                                                                                                                   
[243] Belur Temple                                                                                                                     
[245] HAMPI                                                                                                                            
[247] Badami                                                                                                                           
[249] Pattadakkal                                                                                                                      
[251] Aihole                                                                                                                           
[253] Bijapur                                                                                                                          
[255] Tippu Palace                                                                                                                     
[257] Puthanmalika Palace                                                                                                              
[259] Napier Museum & Art Gallery                                                                                                      
[261] Sri Padmanabha temple  Padmanbhapuram Palace                                           
[263] Safari by Gypsy (Exclusive)                                     
[265] Night Safari by  Gypsy (Exclusive)                              
[267] Safari by Gypsy  (Kanha) Premium Zone(Exclusive)                
[269] Safari by Gypsy  (Kishli, Sarhi & Mukki) Normal Zone Exclusive)
[271] Safari by Gypsy (Exclusive)                                     
[273] Night Safari by  Gypsy                                          
[275] Bandhavgarh National Park:--                                    
[277] Safari by Gypsy(From Tala Gate)Premium Zone Exclusive                                                                            
[279] Safari by Gypsy (From Ghondi & Maghdi Gate) Exclusive                                                                            
[281] Park Safari by Gypsy(Sitabani,Bizrani,Jhirna gate)              
[283] Exclusive for 02 person                                                                                                          
[285] Park Safari by  Gypsy (Durga Devi Gate)                                                                                          
[287] Park Safari by Canter                                           
[289]                                                                                                                                  
[291] Elephant ride in side of the park (Sub to Availability)                                           
[293] For 02 person for 01 hour                                                                                                        
[295]                                                                                                                                  
[297] Park Safari by Gypsy Exclusive for 02 Person                                         
[299] Extra Person (Up to 06 person in one Jeep)                      
[301] Park Safari by Canter-minimum 20 pax                            
[303]   

1 个答案:

答案 0 :(得分:0)

我不确定我是否完全理解你的问题,但它只是删除你想要实现的空值吗?如果是这样,我认为问题是你有一些不是“”的奇怪值,因此当你试图寻找那些值时,R找不到它们。尝试这样的事情:

使用data.table包

library(rvest)
library(data.table)
Holidays <- read_html("http://www.spholidays.com/monuments.php")
a = data.table(Places = html_text(html_nodes(Holidays,".cat-hd1")))
b = data.table(Monuments_Name = html_text(html_nodes(Holidays,"tr~ tr+ tr td:nth-child(1)")))
b=b[-which(b[,Monuments_Name]==b[2,Monuments_Name] | b[,Monuments_Name]==b[281,Monuments_Name] | b[,Monuments_Name]==b[288,Monuments_Name] )]

使用data.frame

c = data.frame(Places = html_text(html_nodes(Holidays,".cat-hd1")))
d = data.frame(Monuments_Name = html_text(html_nodes(Holidays,"tr~ tr+ tr td:nth-child(1)")))
d$Monuments_Name=as.character(d$Monuments_Name)
d=data.frame(Monuments_Name=d$Monuments_Name[-which(d$Monuments_Name==d$Monuments_Name[2] | d$Monuments_Name==d$Monuments_Name[281]  | d$Monuments_Name==d$Monuments_Name[288])])

它对我有用,请告诉我你是否有任何问题。