繁体   English   中英

为什么用For循环进行R模式匹配时为什么没有输出

[英]Why there is no output when I do R Pattern Matching with For loop

这是我第一次编写R函数。 我想要做的是例如我有一个R data.frame这样

                             Vars        match
1                             A_m          0
2                             B_m          0
3                               C          0
4                               D          34
5                               E_m        0

这是我匹配两个数据帧后得到的结果。 match列中,如果数字为0,则表示Vars列中的值不匹配。 例如,在第一行中,A_m在匹配列中为0。 这意味着A_m没有匹配项。 所以我的功能是在Vars列中找到不匹配的值(在match列中为0)。 之后,如果我找到的值以“ _m”结尾,那么它就是我想要的值,我想将它们打印出来。

这是我写的代码。 因为我以前从未编写过R函数,所以我的代码可能存在很多问题。 我想使用data.frame作为函数的参数,并使用for循环检查整个dataframe。 在for循环中,我想使用if来决定是否为目标值。 非常感谢您的帮助和耐心。

varsConvert <- function(x){
  for(i in 1:nrow(x)){
    #x[i,1]is the cordinate of the value in that dataframe, can I write like this?
    if(x[i,1] == 0){
      #I want to match ends with _m by *_m
      if(x[i,0] == "*_m"){
        print(x[i,0])
      }
      else if(x[i,0] == "E"){
          print(x[i,0])
        }
      else{
        stop("this is an error")
      }

    }
  }
}

在我的示例中,我要打印的值应为A_m,B_m和E_m

虽然可以在R中for循环编写“经典”,但通常最好使用其他功能,这些功能会更短/更干净/更快。 这是您的数据(我将其命名为df ):

df<-structure(list(Vars = c("A_m", "B_m", "C", "D", "E_m"), match = c(0L, 
0L, 0L, 34L, 0L)), .Names = c("Vars", "match"), class = "data.frame", row.names = c(NA, 
-5L))

你可以做 :

temp<-df$Vars[df$match==0] # find the names of Vars for which match is equal to 0
temp[grep('_m',temp)] # only select those with _m
# [1] "A_m" "B_m" "E_m"

另一种选择是在Vars中选择match == 0和_m的交点的索引:

df$Vars[intersect(grep('_m',df$Vars),which(df$match==0))]
# [1] "A_m" "B_m" "E_m"

还有另一种方法(使用布尔算术而不是集合操作):

df$Vars[ grepl('_m',df$Vars) & df$match==0 ]

如果要在输入中包含data.frame的函数,则可以执行此操作(我这次使用列号来显示其他可能性):

f<-function(data){
    temp<-data[,1][data[,2]==0]
    temp[grep('_m',temp)]
}

要使用它,请调用f(nameOfYourData)

f(df)
# [1] "A_m" "B_m" "E_m"

暂无
暂无

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

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