2016-05-13 5 views
0

Das Problem: lächerlich große Zahlen innerhalb von Elementen eines Zeichenvektors.Ein regulärer Ausdruck zum Entfernen von RIDICULOUSLY großen Zahlen aus einem Zeichenvektor in R

troublesome_tweets 
[1] "Happi Pi Day! Eat a pizza and some blueberry pie a la mode, and then you may calculate: 3.1415926535897932384626433832795028841971693993751" 
[2] "Sick of this yet? ...2847564823378678316527120190914564856692346034861045432 6648213393607260249141273724587006606315588174881520920..."  
[3] "He only did that 939478978398894709409784680708937809648608378964809798369864 x to the Cavs in that one series"        
[4] "Am I a robot? Yes, affirmative. 0111001001101111011000100110111100100000011000100110111101101111011001110110100101100101"     
[5] "Lazy rule # 18460826292036391273639018263920273820183737473920383930; You were too lazy to read the whole number .(:"      
[6] "#thankyoushootusdown I LOVE YOU GUYS <3333333333333333333333333333333333333333333333333333333333333333333333333333333333333"     
[7] "Hvrtujikdsjktrfedwqcvbntrfedsw1123456787654325678876543234567865432456765432345678654214565tredscvbhjutwsdfvghyu. ! How I feel."    
[8] "I want to dock 555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555"        
[9] "x's to the club wit momz here 0 while gone 1000000000000000000000000000000000000000000000000000000000000000"         

Der derzeitige Ansatz: nach jedem Elemente gehen und verwendet gsub zusammen mit einem bestimmten regulären Ausdruck die große Zahl aus einem gegebenen bestimmten Elemente zu entfernen.

clean_individual_tweets <- function(x){ 
    x <- gsub("[0-9][.][0-9]+", " ", x) 
    x <- gsub("[...][0-9]+", "", x) 
    x <- gsub("[0-9]+[...]", "", x) 
    x <- gsub("[0-9]+[ ][x]", "", x) 
    x <- gsub("[.][ ][0-9]+", " ", x) 
    x <- gsub("[#][ ][0-9]+", " ", x) 
    x <- gsub("[<][0-9]+", " ", x) 
    x <- gsub("[a-zA-Z][0-9]+", " ", x) 
    x <- gsub("555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555", " ", x) 
    x <- gsub("1000000000000000000000000000000000000000000000000000000000000000", " ", x)} 


cleaned_tweets <- clean_individual_tweets(troublesome_tweets 

cleaned_tweets 
[1] "Happi Pi Day! Eat a pizza and some blueberry pie a la mode, and then you may calculate: " 
[2] "Sick of this yet? .. .."                 
[3] "He only did that to the Cavs in that one series"           
[4] "Am I a robot? Yes, affirmative "               
[5] "Lazy rule ; You were too lazy to read the whole number .(:"        
[6] "#thankyoushootusdown I LOVE YOU GUYS "             
[7] "Hvrtujikdsjktrfedwqcvbntrfeds tredscvbhjutwsdfvghyu. ! How I feel."      
[8] "I want to dock "                   
[9] "x's to the club wit momz here 0 while gone "  

Die für Ansatz erhofft: einen einzigen regulären Ausdruck zu haben, die alle diese großen Zahlen und andere von großen Zeichenvektoren entfernen können, lassen Sie sich eine beliebige Anzahl von mehr als 10 Ziffern zusammengesetzt ersetzen sagen. Ich möchte nicht alle Nummern löschen, das wäre sehr einfach anzufangen. Ich möchte die Artefaktnummern gezielt entfernen und die nicht-Artefakten behalten.

Daten

troublesome_tweets <- c(
    "Happi Pi Day! Eat a pizza and some blueberry pie a la mode, and then you may calculate: 3.1415926535897932384626433832795028841971693993751" 
, "Sick of this yet? ...2847564823378678316527120190914564856692346034861045432 6648213393607260249141273724587006606315588174881520920..."  
, "He only did that 939478978398894709409784680708937809648608378964809798369864 x to the Cavs in that one series"        
, "Am I a robot? Yes, affirmative. 0111001001101111011000100110111100100000011000100110111101101111011001110110100101100101"     
, "Lazy rule # 18460826292036391273639018263920273820183737473920383930; You were too lazy to read the whole number .(:"      
, "#thankyoushootusdown I LOVE YOU GUYS <3333333333333333333333333333333333333333333333333333333333333333333333333333333333333"     
, "Hvrtujikdsjktrfedwqcvbntrfedsw1123456787654325678876543234567865432456765432345678654214565tredscvbhjutwsdfvghyu. ! How I feel."    
, "I want to dock 555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555"        
, "x's to the club wit momz here 0 while gone 1000000000000000000000000000000000000000000000000000000000000000"         
) 
+0

cleaned_tweets <- clean_individual_tweets (troublesome_tweets) # fehlende Klammer in dem obigen Code – mansanto

+3

'gsub ('\\ d {2,} ',' ', lästige_Tüpfelchen) '? oder '\\ d {2,} | \\ d + \\. \\ d {2,} '' um die pi-Dezimalzahl loszuwerden – rawr

+0

Oder '(?: \\ d + \\.)? \ \ d {2,} ' –

Antwort

0

Für 'mindestens 10 Stellen', versuchen Sie folgendes:

gsub("[0-9]{10,}","",troublesome_tweets) 

[1] "Happi Pi Day! Eat a pizza and some blueberry pie a la mode, and then you may calculate: 3." 
[2] "Sick of this yet? ... ..."                 
[3] "He only did that x to the Cavs in that one series"           
[4] "Am I a robot? Yes, affirmative. "               
[5] "Lazy rule # ; You were too lazy to read the whole number .(:"        
[6] "#thankyoushootusdown I LOVE YOU GUYS <"              
[7] "Hvrtujikdsjktrfedwqcvbntrfedswtredscvbhjutwsdfvghyu. ! How I feel."       
[8] "I want to dock "                   
[9] "x's to the club wit momz here 0 while gone " 
+0

danke Eric für Ihre Antwort. – mansanto

Verwandte Themen