gpt4 book ai didi

regex - 在 r 中的正则表达式中粘贴带引号的变量

转载 作者:行者123 更新时间:2023-12-05 07:50:25 25 4
gpt4 key购买 nike

我的问题的核心是使用“粘贴”将变量放入正则表达式中,并使用转义字符作为引号。这是其他 stackoverflow 问题的答案——但它似乎不起作用。我认为这个问题与那些问题的独特之处在于它结合了两个元素——使用粘贴将变量放入正则表达式中,并使用转义字符作为引号。我也尝试过使用“猫”,这是另一个常见的答案,但没有成功。

我正在尝试做的是使通过包含数千个名称的列表按名称进行过滤变得更加容易。 (我正在从可视化软件 (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)
}

最佳答案

要解决特定问题,请将 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)
}

“选择一个数字”提示可能会起作用,因为没有打印行号,但至少它起作用了。

关于regex - 在 r 中的正则表达式中粘贴带引号的变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36205267/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com