矢量化自定义函数

时间:2016-03-10 15:29:25

标签: r function vectorization

我编写了这个函数来返回某个字符之前的字符串,如下所示:

strBefore <- function(find, x, last = FALSE, occurence) {

  # Checking.
  if (class(x)[1] != "character") { stop("The strBefore function only supports objects of character class.") }

  # Getting the place of the find, and handling both caes of last.
  fullPlace <- gregexpr(find, x)[[1]] # Gets the location of the occurences of find in x.

  # Handling the case where last is TRUE.
  if (last == TRUE) { place <- max(fullPlace) # Grabbing the latest character index if last is TRUE.
  } else { place <- min(fullPlace) } # Otherwise, getting the first space.

  # Handles the occurrenceargument if given.
  if (!missing(occurrence)) { place <- fullPlace[occurrence] }

  # Subsetting the string.
  xlen <- nchar(x) # Getting the total number of characters in the string.
  x <- substr(x, 1, place - 1) # Minus 1 because we don't want to include the first hit for find.
  return(x)

}

find是您之前想要字符串的字符,x是字符,last询问您是否在最后一次出现find之前获取,{ {1}}指定之前获得的occurrence出现次数(如果给定则覆盖find)。

如果我在单个角色对象上使用它,它可以正常工作:

last

但是,如果我在字符向量上使用它,它会将向量中的每个项目剪切到与第一个项目相同的位置:

> test <- "Hello World"
> test2 <- strBefore(" ", test)
> test2
[1] "Hello"

现在,这里的链接确实为我提供了一个实现我想要的方法:

Using gsub to extract character string before white space in R

但是,我确实喜欢&#34;发生&#34;的功能。参数,返回查找参数的第2个,第3个等之前的字符串。

就像一张纸条,我可以像> test <- c("Hello World", "Hi There", "Why Hello") > test2 <- strBefore(" ", test) > test2 [1] "Hello" "Hi Th" "Why H" 那样向我的函数进行矢量化:

sapply

这在某种程度上解决了我的问题...但是有没有办法在不使用> test <- c("Hello World", "Hi There", "Why Hello") > test2 <- sapply(test, function(x) strBefore(" ", x)) > test2 Hello World Hi There Why Hello "Hello" "Hi" "Why" 函数的情况下更干净地完成这项工作? 我不是在寻找apply所做的解决方案,而是更多关于如何向量化自定义函数的解决方案。谢谢你的时间。

0 个答案:

没有答案