另一個版本是你的「字」翻譯成raw()
載體與'字母'作爲raw()
矢量進行比較。
frequency = function(word, letter)
sum(charToRaw(word) == charToRaw(letter))
這裏有四個不同的解決方案
f0 <- function(word, letter)
sum(strsplit(word, "")[[1]] == letter)
f1 <- function(word, letter)
sum(charToRaw(word) == charToRaw(letter))
f2a <- function(word, letter)
length(unlist(gregexpr(letter, word)))
f2b <- function(word, letter)
length(unlist(gregexpr(letter, word, fixed=TRUE)))
一些正確性和性能比較
> word <- "foo"
> letter <- "o"
> identical(f0(word, letter), f1(word, letter))
[1] TRUE
> identical(f0(word, letter), f2a(word, letter))
[1] TRUE
> identical(f0(word, letter), f2b(word, letter))
[1] TRUE
> letter <- "a"
> identical(f0(word, letter), f1(word, letter))
[1] TRUE
> identical(f0(word, letter), f2a(word, letter))
[1] FALSE
> identical(f0(word, letter), f2b(word, letter))
[1] FALSE
> word <- paste(sample(letters, 10000, TRUE), collapse="")
> letter <- "a"
> microbenchmark(
+ f0(word, letter), f1(word, letter),
+ f2a(word, letter), f2b(word, letter)
+)
Unit: microseconds
expr min lq mean median uq max neval
f0(word, letter) 558.433 562.4755 579.03451 583.5590 584.8920 628.946 100
f1(word, letter) 71.482 78.7100 100.85787 80.0275 81.7035 2195.366 100
f2a(word, letter) 277.618 278.7280 280.94280 279.4870 280.4270 302.683 100
f2b(word, letter) 66.888 68.1800 69.07205 68.6205 69.3100 84.300 100
f2b()
是最快的,但也是不正確;目前看起來似乎很快(儘管速度對於手頭的任務來說可能並不重要)並且是正確的。
而不是你的循環,嘗試'sum(word == y)'。哦,但是你看到的問題是因爲你需要做'strsplit(x,「」)[[1]]或者換成unlist。 – Frank
謝謝,你能解釋爲什麼需要添加[[1]]]? –
您需要在最後返回一個值。 'print'將值打印到屏幕上,但返回NULL對象。用'counter'或'return(counter)'替換'print(counter)'。 – lmo