在r中使用regex中的引号粘贴变量

时间:2016-03-24 16:30:58

标签: regex r

我的问题的核心是使用“粘贴”将变量放在正则表达式中,并使用转义字符作为引号。这是其他stackoverflow问题的答案 - 但它似乎并没有起作用。我认为这个问题的独特之处在于它将两个元素组合在一起 - 使用粘贴将变量放在正则表达式中,并使用转义字符作为引号。我也尝试过使用“cat”,这是另一个经常回答的问题,没有运气。

我要做的是通过名单中的数千个名称来更轻松地按名称进行过滤。 (我正在从可视化软件(Spotfire)转到R,我想念列表框过滤器。)我对如何执行此任务的建议非常满意。

而且,是的,我是R编程的新手(以及一般的编程。)Stackoverflow是绝对最好的资源。你们一定是一群天才,

谢谢 -

# 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)
}

1 个答案:

答案 0 :(得分:0)

要解决特定问题,请将apxname引用为变量,而不是字符串。清理了一点,包括上面Mathias提到的拼写错误,并在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)
}

此版本返回车名;如果你想要返回汽车的所有统计数据,你需要像

这样的东西
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)
}

&#34;选择一个号码&#34;提示可以使用工作,因为没有打印行号,但它至少可以工作。