2016-03-24 14 views
2

Der Kern meiner Frage ist "Einfügen" verwenden, um eine Variable innerhalb einer Regex mit Escape-Zeichen für Anführungszeichen setzen. Dies ist die Antwort auf andere Stackoverflow-Fragen - aber es scheint einfach nicht zu funktionieren. Was ich denke, dass diese Frage einzigartig ist, ist, dass es beide Elemente kombiniert - mit Einfügen, um eine Variable in Regex zu setzen, und mit Escape-Zeichen für Anführungszeichen. Ich habe auch versucht, "Katze", eine andere häufige Antwort, ohne Glück zu verwenden.Einfügen einer Variablen mit Anführungszeichen innerhalb von Regex in r

Was ich versuche zu tun, ist es einfacher, nach Namen durch eine Liste von Tausenden von Namen zu filtern. (Ich gehe von der Visualisierungssoftware (Spotfire) zu R und ich vermisse die Listbox-Filter.) Ich wäre sehr glücklich mit jedem Rat, wie ich diese Aufgabe ausführen soll.

Und ja, ich bin neu in der R-Programmierung (und Programmierung im Allgemeinen.) Stackoverflow war die überwältigend beste Ressource. Y'all muss da draußen ein paar Genies sein,

Thank Sie--

# mtcars example for filtering and finding names for the stack overflow question 

data(mtcars) 

# make the data match my dataframe, where I don't have row names but have a column with the name 
mtcars$carname <- NA #declare the variable 
mtcars$carname <- rownames(mtcars) #assign the names to a column 

findcar <- function() { 

    while(TRUE) { 
    print("Type the car's name:")  
    apxname <- readline() #approx name 
    #type in Merc for this example 
    carlst <- mtcars$carname[(grepl("(apxname)",mtcars$carname, ignore.case = TRUE))] #list of cars that matches the approximate name 
      # if I type . . . (grepl("(Merc)", mtcars$carname, . . .)) it works great 
    #So per other stackoverflow responses, I've tried using "paste" or "paste0" without success 
    #I can't get this to work 
    #carlst <- mtcars$carname[(grepl(paste0('\"(',apxname, ')\"', sep=""),mtcars$carname, ignore.case = TRUE))] 
    print("Here's the list of similar customers:") 
    print(carlst) 
    print("Type the number of your car:") 
    carnum <- readline() #car number 
    therightone <- carlst[as.numeric(carnum)] 
    paste("You selected",therightone,"Is this the car (Y/N)?", sep=" ") 
    carconf <- readline() #car confirmation 
    if(carconf == "Y") break) 
    } 
return(therightone) 
} 
+0

Bitte erweitern Sie "aber es scheint einfach nicht zu funktionieren". Vielen Dank. –

+0

Verwenden Sie 'carlst <- mtcars $ carname [(grepl (get (" apxname "), x = mtcars $ carname, ignore.case = TRUE))]'. – matthias

+0

Ich kann das nicht bearbeiten, weil es nur ein Zeichen ist, aber Sie müssen auch die Klammer nach der 'break' Anweisung löschen. – matthias

Antwort

0

Um das besondere Problem zu beheben, Referenz apxname als Variable, kein String. Gereinigt ein wenig mehr, einschließlich der Tippfehler von Mathias oben erwähnt, und das Einwickeln der Autobestätigungsaufforderung in print:

findcar <- function() { 

    while(TRUE) { 
     print("Type the car's name:")  
     apxname <- readline() # approx name 
     # list of cars that matches the approximate name 
     carlst <- rownames(mtcars)[grepl(apxname, rownames(mtcars), ignore.case = TRUE)] 
     print("Here's the list of similar customers:") 
     print(carlst) 
     print("Type the number of your car:") 
     carnum <- readline() # car number 
     therightone <- carlst[as.numeric(carnum)] 
     print(paste("You selected",therightone,"Is this the car (Y/N)?", sep=" ")) 
     carconf <- readline() # car confirmation 
     if(carconf == "Y") break 
    } 
    return(therightone) 
} 

Diese Version kehrt Auto-Namen; wenn Sie alle Statistiken für das Auto zurückgeben möchten, müssen Sie so etwas wie

findcar <- function() { 

    while(TRUE) { 
     print("Type the car's name:")  
     apxname <- readline() # approx name 
     # list of cars that matches the approximate name 
     carlst <- mtcars[grepl(apxname, rownames(mtcars), ignore.case = TRUE),] 
     print("Here's the list of similar customers:") 
     print(carlst) 
     print("Type the number of your car:") 
     carnum <- readline() # car number 
     therightone <- carlst[as.numeric(carnum),] 
     print(paste("You selected",rownames(therightone),"Is this the car (Y/N)?", sep=" ")) 
     carconf <- readline() # car confirmation 
     if(carconf == "Y") break 
    } 
    return(therightone) 
} 

Die „Wählen Sie eine Nummer“ prompt Arbeit nutzen könnten, da keine Zeilennummern gedruckt werden, aber es funktioniert zumindest.

Verwandte Themen