简体   繁体   English

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

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

This is the first time I wrote a R function. 这是我第一次编写R函数。 What I want to do is for example I have a R data.frame like this 我想要做的是例如我有一个R data.frame这样

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

This is the result I got after matching two dataframes. 这是我匹配两个数据帧后得到的结果。 In the match column, if the number is 0, it means there is no match for the value in the Vars column. match列中,如果数字为0,则表示Vars列中的值不匹配。 For example in first row, A_m has 0 in match column. 例如,在第一行中,A_m在匹配列中为0。 This means A_m doesn't have a match. 这意味着A_m没有匹配项。 So my function is to find those values in Vars column that has no match (has 0 in match column). 所以我的功能是在Vars列中找到不匹配的值(在match列中为0)。 After that if the value I found ends with "_m" then it is the value I want, and I want to print them out. 之后,如果我找到的值以“ _m”结尾,那么它就是我想要的值,我想将它们打印出来。

This is the code I wrote. 这是我写的代码。 Because I have never written R function before, there can be lots of problems with my code. 因为我以前从未编写过R函数,所以我的代码可能存在很多问题。 I want to use data.frame as my function's argument and use for loop to check the whole dataframe. 我想使用data.frame作为函数的参数,并使用for循环检查整个dataframe。 In the for loop I want to use if to decide whether it is the target value or not. 在for循环中,我想使用if来决定是否为目标值。 Really appreciate for your help and Patience. 非常感谢您的帮助和耐心。

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")
      }

    }
  }
}

In my example the values I want to print should be A_m, B_m and E_m 在我的示例中,我要打印的值应为A_m,B_m和E_m

While it is possible to write "classic" for loops in R, it's often best to use other functions which will be shorter/cleaner/faster. 虽然可以在R中for循环编写“经典”,但通常最好使用其他功能,这些功能会更短/更干净/更快。 Here is your data (I named it df ) : 这是您的数据(我将其命名为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))

You can do : 你可以做 :

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"

An other option is to select the indexes of the intersection of match==0 and _m in Vars : 另一种选择是在Vars中选择match == 0和_m的交点的索引:

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

And yet another approach (using Boolean arithmetic rather than set operations): 还有另一种方法(使用布尔算术而不是集合操作):

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

If you want a function with a data.frame in input you can do this (I used column numbers this time to show other possibilities) : 如果要在输入中包含data.frame的函数,则可以执行此操作(我这次使用列号来显示其他可能性):

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

To use it, call f(nameOfYourData) : 要使用它,请调用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