英文:
How to use grep or grepl in order to identify variables?
问题
下午好。以下是情况:
我有一个名为"dep"的变量,定义如下:
dep <- c("txn_amt", "txn_cnt", "acct_cnt")
我将fn_form定义为:
fn_form = c(dep, paste("log(", dep, "+1)", sep=""), paste("log(", dep, ")", sep=""))
当我运行:
fnform <- "count"
if (length(grep("log", fn_form)) > 0) {
fnform <- "log"
}
if (grepl("log\\(\\+1\\)", fn_form) > 0) {
fnform <- "log+1"
}
它没有保存"count"、"log"和"log+1"。它只保存了"count"和"log",因为R没有识别出"log"和"log+1"之间的区别。我该如何修复这个问题,以便保存这三个值?
英文:
good afternoon. Here is the situation:
I have a variable called "dep" that is defined as:
dep <- c("txn_amt", "txn_cnt", "acct_cnt")
I defined fn_form as:
fn_form = c(dep, paste("log(",dep,"+1)",sep=""),paste("log(",dep,")",sep=""))
When I ran:
fnform <- "count"
if(length(grep("log",fn_form))>0) {
fnform <- "log"
}
if(grepl("log\\(\\+1\\)",fn_form)>0) {
fnform <- "log+1"
}
it is not saving "count", "log" and "log+1". It is only saving "count" and "log" because R is not recognizing the difference between log and log+1. How can I fix this in order to save for these 3 values?
答案1
得分: 1
有一些问题。首先,您尝试评估fn_form
的所有元素,但然后将fn_form
替换为单个值。更重要的是,您提出的问题是,您正在搜索"log(+1)",但您应该搜索"log(.*+1)",因为变量名位于附加到log的括号内。以下是我如何做的:
dep <- c("txn_amt", "txn_cnt", "acct_cnt")
fn_form = c(dep, paste("log(",dep,"+1)",sep=""),paste("log(",dep,")",sep=""))
sapply(fn_form, function(x){
out <- "count"
if(grepl("log\\(", x)) out <- "log"
if(grepl("log\\(.*\\+1", x)) out <- "log+1"
out
})
#> txn_amt txn_cnt acct_cnt log(txn_amt+1) log(txn_cnt+1)
#> "count" "count" "count" "log+1" "log+1"
#> log(acct_cnt+1) log(txn_amt) log(txn_cnt) log(acct_cnt)
#> "log+1" "log" "log" "log"
创建于2023年06月01日,使用reprex v2.0.2
英文:
There were a couple of problems. First, you're trying to evaluate all elements of fn_form
, but then replace fn_form
with a single value. More to the point you were raising though, is that you were searching for "log(+1)", when you should have been searching for "log(.*+1)" - because the variable name is inside the parentheses attached to log. Here's how I would do it:
dep <- c("txn_amt", "txn_cnt", "acct_cnt")
fn_form = c(dep, paste("log(",dep,"+1)",sep=""),paste("log(",dep,")",sep=""))
sapply(fn_form, function(x){
out <- "count"
if(grepl("log\\(", x))out <- "log"
if(grepl("log\\(.*\\+1", x))out <- "log+1"
out
})
#> txn_amt txn_cnt acct_cnt log(txn_amt+1) log(txn_cnt+1)
#> "count" "count" "count" "log+1" "log+1"
#> log(acct_cnt+1) log(txn_amt) log(txn_cnt) log(acct_cnt)
#> "log+1" "log" "log" "log"
<sup>Created on 2023-06-01 with reprex v2.0.2</sup>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论