【问题标题】:Find all words that have "<-" at the end of the word OR in front of a dot查找单词末尾或点前带有“<-”的所有单词
【发布时间】:2016-05-14 17:45:43
【问题描述】:

如何提取在单词末尾或介于两者之间的所有带有符号“&lt;-”的单词,但在后一种情况下,仅当“&lt;-”符号后跟一个点时。

把它放到上下文中。练习 6.5.3 a。 Hadley Wickhams - Advanced R 要求读者列出基本包中的所有替换功能。

只有一种方法的替换函数用符号&lt;-表示 就在函数名的末尾。然而,泛型函数有它们的 方法名称附加到替换形式的名称(带点),这样&lt;- 不再位于函数名称的末尾。示例split&lt;-.data.frame

编辑:

obj <- mget(ls("package:base"), inherits = TRUE)
funs <- Filter(is.function, objs)

这是您提取基本包中所有功能的方式。现在我只想找到替换函数。

【问题讨论】:

  • 正则表达式为\b\w+&lt;-(?![\w.])|\b\w+&lt;-\.\w+\b

标签: regex r


【解决方案1】:

如果你想要所有的基础包替换功能及其各自的S3方法,你可以试试

ls(envir = as.environment("package:base"), pattern = "<-")

没有加载任何包,结果如下:

 [1] "<<-"                     "<-"                      "[<-"                    
 [4] "[[<-"                    "@<-"                     "$<-"                    
 [7] "attr<-"                  "attributes<-"            "body<-"                 
[10] "class<-"                 "colnames<-"              "comment<-"              
[13] "[<-.data.frame"          "[[<-.data.frame"         "$<-.data.frame"         
[16] "[<-.Date"                "diag<-"                  "dim<-"                  
[19] "dimnames<-"              "dimnames<-.data.frame"   "Encoding<-"             
[22] "environment<-"           "[<-.factor"              "[[<-.factor"            
[25] "formals<-"               "is.na<-"                 "is.na<-.default"        
[28] "is.na<-.factor"          "is.na<-.numeric_version" "length<-"               
[31] "length<-.factor"         "levels<-"                "levels<-.factor"        
[34] "mode<-"                  "mostattributes<-"        "names<-"                
[37] "names<-.POSIXlt"         "[<-.numeric_version"     "[[<-.numeric_version"   
[40] "oldClass<-"              "parent.env<-"            "[<-.POSIXct"            
[43] "[<-.POSIXlt"             "regmatches<-"            "row.names<-"            
[46] "rownames<-"              "row.names<-.data.frame"  "row.names<-.default"    
[49] "split<-"                 "split<-.data.frame"      "split<-.default"        
[52] "storage.mode<-"          "substr<-"                "substring<-"            
[55] "units<-"                 "units<-.difftime"

感谢 @42 帮助我改进这个答案。

【讨论】:

    【解决方案2】:

    我们可以试试

    library(stringr)
    str_extract(v1, "\\w+<-$|\\w*<-\\.\\S+")
    #[1] "split<-.data.frame" NA                   "splitdata<-"    
    

    数据

    v1 <-  c("split<-.data.frame", "split<-data", "splitdata<-")
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-08-20
      • 2011-02-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多