如何使用grep或grepl来识别变量?

huangapple go评论142阅读模式
英文:

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 &lt;- c(&quot;txn_amt&quot;, &quot;txn_cnt&quot;, &quot;acct_cnt&quot;)

I defined fn_form as:

fn_form = c(dep, paste(&quot;log(&quot;,dep,&quot;+1)&quot;,sep=&quot;&quot;),paste(&quot;log(&quot;,dep,&quot;)&quot;,sep=&quot;&quot;))

When I ran:

fnform &lt;- &quot;count&quot;
  if(length(grep(&quot;log&quot;,fn_form))&gt;0) {
   fnform &lt;- &quot;log&quot;
 }
   if(grepl(&quot;log\\(\\+1\\)&quot;,fn_form)&gt;0) {
  fnform &lt;- &quot;log+1&quot;
 }

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 &lt;- c(&quot;txn_amt&quot;, &quot;txn_cnt&quot;, &quot;acct_cnt&quot;)
fn_form = c(dep, paste(&quot;log(&quot;,dep,&quot;+1)&quot;,sep=&quot;&quot;),paste(&quot;log(&quot;,dep,&quot;)&quot;,sep=&quot;&quot;))

sapply(fn_form, function(x){
  out &lt;- &quot;count&quot;
  if(grepl(&quot;log\\(&quot;, x))out &lt;- &quot;log&quot;
  if(grepl(&quot;log\\(.*\\+1&quot;, x))out &lt;- &quot;log+1&quot;
  out
})
#&gt;         txn_amt         txn_cnt        acct_cnt  log(txn_amt+1)  log(txn_cnt+1) 
#&gt;         &quot;count&quot;         &quot;count&quot;         &quot;count&quot;         &quot;log+1&quot;         &quot;log+1&quot; 
#&gt; log(acct_cnt+1)    log(txn_amt)    log(txn_cnt)   log(acct_cnt) 
#&gt;         &quot;log+1&quot;           &quot;log&quot;           &quot;log&quot;           &quot;log&quot;

<sup>Created on 2023-06-01 with reprex v2.0.2</sup>

huangapple
  • 本文由 发表于 2023年6月2日 02:14:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/76384630.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定