简体   繁体   English

在 R 的 for 循环中的 if else 语句中使用 grepl()

[英]Using grepl() within an if else statement within a for loop in R

I am trying to use grepl() within an if else string within a for loop in order to do something I figure is pretty common in R: I have one column that has long names for plots, and I am trying to take information from that plot column and generate new columns based on that information.我试图在 for 循环中的 if else 字符串中使用 grepl() 以执行我认为在 R 中很常见的事情:我有一列有很长的绘图名称,我试图从中获取信息绘制列并根据该信息生成新列。 For this example, I will provide a version of my data to explain what I have tried:对于这个例子,我将提供我的数据版本来解释我的尝试:

for(i in seq_along(data)) {
  if(grepl("1987", data$id) == TRUE) {
    print("dnr87")
  } else if { (grepl("1994", data$id) == TRUE) {
else
print("no id") 
}

I receive the warning:我收到警告:

the condition has length > 1 and only the first element will be used

My questions are:我的问题是:

  1. I understand that an if else statement designed this way only works on a single object, but since I nested it within a for loop, shouldn't it proceed to apply its process to each row?我知道以这种方式设计的 if else 语句仅适用于单个对象,但是由于我将它嵌套在 for 循环中,它不应该继续将其过程应用于每一行吗?
  2. Am I using grepl correctly?我是否正确使用了 grepl? Shouldn't this effectively pick up on the features I am targeting?这不应该有效地了解我的目标功能吗?
  3. How can I get this data to output into the corresponding rows into my empty column called "plot" below?我怎样才能让这些数据输出到相应的行到我下面称为“绘图”的空列中? This is the least of my concerns, because if I am able to get it to spit out the data, I can just merge the data into that column if needed.这是我最不担心的,因为如果我能够让它吐出数据,我可以在需要时将数据合并到该列中。

Obviously, as seen in the data below, I would like to design the for loop to also go through and assign the same kind of data not only based on a grepl for year, but a grepl for ID (for example, grepl("E14) for the first data point), but I figure if you all can help me with the previous mentioned questions I should be able to incorporate that without huge issues.显然,如下面的数据所示,我想设计 for 循环来遍历和分配相同类型的数据,不仅基于年份的 grepl,而且是 ID 的 grepl(例如,grepl("E14 ) 作为第一个数据点),但我想如果你们都可以帮助我解决前面提到的问题,我应该能够在没有大问题的情况下合并它。

Data table (name: data)数据表(名称:数据)

id  stand   plot
E141987 NA  NA
E141987 NA  NA
E141987 NA  NA
E141987 NA  NA
E121994 NA  NA
E121994 NA  NA
E121994 NA  NA
E121994 NA  NA
E121994 NA  NA
E121994 NA  NA
E121994 NA  NA
E121994 NA  NA
W52012  NA  NA
W52012  NA  NA
W52012  NA  NA
W52012  NA  NA
W52012  NA  NA
W52012  NA  NA

Since you're not providing reproducible data to illustrate the procedure, here's some mock data.由于您没有提供可重现的数据来说明该过程,因此这里提供一些模拟数据。 If I understand you correctly you want to mutate codes based on patterns.如果我理解正确,您想根据模式改变代码。 If that's the case you can use nested ifelse statements :如果是这种情况,您可以使用嵌套的 ifelse 语句

Data:数据:

df <- data.frame(
  id = c("a87", "b87", "abc95", "a95", "x123")
)

Now you define the new column with the mutated values:现在您使用变异值定义新列:

df$new <- ifelse(grepl("87", df$id), "new1",
                 ifelse(grepl("95", df$id), "new2", "new3"))

An ifelse clause takes three arguments as input: (i) the condition, (ii) what to do if the condition holds true, (iii) what to do if it doesn't. ifelse子句接受三个参数作为输入:(i) 条件,(ii) 如果条件成立怎么办,(iii) 如果不成立怎么办。 The execution of (iii) can be delayed by inserting yet another ifelse clause testing for a second condition.可以通过插入另一个ifelse子句测试第二个条件来延迟 (iii) 的执行。 This in turn can be delayed yet again by a third condition and so on.这反过来又会因第三种情况等而再次延迟。

The result:结果:

df
     id  new
1   a87 new1
2   b87 new1
3 abc95 new2
4   a95 new2
5  x123 new3

These are not your data but I guess you get the gist: there's no need for a for loop and you can nest one ifelse in another.这些不是你的数据,但我想你明白了要点:不需要for循环,你可以将一个ifelse嵌套在另一个中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM