gsubfn: Utilities for strings and function arguments

> library(gsubfn)

バージョン: 0.6.6


関数名 概略
as.function.formula Make a one-line function from a formula.
fn Transform formula arguments to functions.
gsubfn Pattern Matching and Replacement
gsubfn-package gsubfn
match.funfn Generic extended version of R match.fun
read.pattern Read file or text string using a regular expression to separate fields.
strapply Apply a function over a string or strings.

fn

> fn$lapply(list(1:4, 1:3), ~ LETTERS[x])
[[1]]
[1] "A" "B" "C" "D"

[[2]]
[1] "A" "B" "C"

gsubfn

パターンマッチによる文字の置換

> gsubfn(pattern     = "[[:digit:]]+", 
+        replacement = function(x) as.numeric(x) + 1, 
+        x           = "(10 20)(100 30)")
[1] "(11 21)(101 31)"
> gsubfn("([0-9]+):([0-9]+)", 
+        ~ as.numeric(x) + as.numeric(y), 
+        "abc 10:20 def 30:40 50")
[1] "abc 30 def 70 50"

read.pattern

> Lines <- "this is the first field 1 2
+ more text 3 4
+ "
> pat <- "^(.*) +(\\S+) +(\\S+)$"
> read.pattern(text = Lines, pattern = pat, as.is = TRUE)
                       V1 V2 V3
1 this is the first field  1  2
2               more text  3  4

strapply

> # 数値を区切っていく
> strapply("12;34:56,89,,123", "[0-9]+")
[[1]]
[1] "12"  "34"  "56"  "89"  "123"
> strapply("a:b c:d", "(.):(.)", c)[[1]]
[1] "a" "b" "c" "d"