- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
计算R中向量中每个唯一元素的出现次数的最快方法是什么?
到目前为止,我已经尝试了以下五个功能:
f1 <- function(x)
{
aggregate(x, by=list(x), FUN=length)
}
f2 <- function(x)
{
r <- rle(x)
aggregate(r$lengths, by=list(r$values), FUN=sum)
}
f3 <- function(x)
{
u <- unique(x)
data.frame(Group=u, Counts=vapply(u, function(y)sum(x==y), numeric(1)))
}
f4 <- function(x)
{
r <- rle(x)
u <- unique(r$values)
data.frame(Group=u, Counts=vapply(u, function(y)sum(r$lengths[r$values==y]), numeric(1)))
}
f5 <- function(x)
{
as.data.frame(unclass(rle(sort(x))))[,2:1]
}
microbenchmark
):
> x <- sample(1:100, size=1e3, TRUE); microbenchmark(f1(x), f2(x), f3(x), f4(x), f5(x))
Unit: microseconds
expr min lq median uq max neval
f1(x) 4133.353 4230.3700 4272.5985 4394.1895 7038.420 100
f2(x) 4464.268 4549.8180 4615.3465 4728.1995 7457.435 100
f3(x) 1032.064 1063.0080 1091.7670 1135.4525 3824.279 100
f4(x) 4748.950 4801.3725 4861.2575 4947.3535 7831.308 100
f5(x) 605.769 696.9615 714.9815 729.5435 3411.817 100
>
> x <- sample(1:100, size=1e4, TRUE); microbenchmark(f1(x), f2(x), f3(x), f4(x), f5(x))
Unit: milliseconds
expr min lq median uq max neval
f1(x) 25.057491 25.739892 25.937021 26.321998 27.875918 100
f2(x) 27.223552 27.718469 28.023355 28.537022 30.584403 100
f3(x) 5.361635 5.458289 5.537650 5.657967 8.261243 100
f4(x) 35.341726 35.841922 36.299161 38.012715 70.096613 100
f5(x) 2.158415 2.248881 2.281826 2.384304 4.793000 100
>
> x <- sample(1:100, size=1e5, TRUE); microbenchmark(f1(x), f2(x), f3(x), f4(x), f5(x), times=10)
Unit: milliseconds
expr min lq median uq max neval
f1(x) 236.53630 240.93358 242.88631 244.33994 250.75403 10
f2(x) 261.03280 263.61096 264.67032 265.81852 297.92244 10
f3(x) 53.94873 55.59020 59.05662 61.05741 87.23288 10
f4(x) 385.10217 390.44888 396.40572 399.23762 432.47262 10
f5(x) 18.31358 18.53492 18.84327 20.22700 20.34385 10
>
> x <- sample(1:100, size=1e6, TRUE); microbenchmark(f1(x), f2(x), f3(x), f4(x), f5(x), times=3)
Unit: milliseconds
expr min lq median uq max neval
f1(x) 2559.0462 2568.7480 2578.4498 2693.3116 2808.1734 3
f2(x) 2833.2622 2881.9241 2930.5860 2946.7877 2962.9895 3
f3(x) 743.6939 748.3331 752.9723 778.9532 804.9341 3
f4(x) 4471.8494 4544.6490 4617.4487 4696.2698 4775.0909 3
f5(x) 243.8903 253.2481 262.6058 269.1038 275.6018 3
>
> x <- sample(1:1000, size=1e6, TRUE); microbenchmark(f1(x), f2(x), f3(x), f4(x), f5(x), times=3)
Unit: milliseconds
expr min lq median uq max neval
f1(x) 2614.7104 2634.9312 2655.1520 2701.6216 2748.0912 3
f2(x) 3038.0353 3116.7499 3195.4645 3197.7423 3200.0202 3
f3(x) 6488.7268 6508.6495 6528.5722 6836.9738 7145.3754 3
f4(x) 40244.5038 40653.2633 41062.0229 41200.1973 41338.3717 3
f5(x) 244.2052 245.0331 245.8609 273.3307 300.8006 3
> x <- sample(1:10000, size=1e6, TRUE); microbenchmark(f1(x), f2(x), f3(x), f4(x), f5(x), times=3) # SLOW!
Unit: milliseconds
expr min lq median uq max neval
f1(x) 3279.2146 3300.7527 3322.2908 3338.6000 3354.9091 3
f2(x) 3563.5244 3578.3302 3593.1360 3597.2246 3601.3132 3
f3(x) 61303.6299 61928.4064 62553.1830 63089.5225 63625.8621 3
f4(x) 398792.7769 400346.2250 401899.6732 490921.6791 579943.6850 3
f5(x) 261.1835 263.7766 266.3697 287.3595 308.3494 3
f5
,但我想看看它能否胜过...
f6
,由@AdamHyland修改的
f8
和由@dickoa提出的
f9
,这是新的结果:
f6 <- function(x)
{
data.table(x)[, .N, keyby = x]
}
f8 <- function(x)
{
fac <- factor(x)
data.frame(x = levels(fac), freq = tabulate(as.integer(fac)))
}
f9 <- plyr::count
> x <- sample(1:1e4, size=1e6, TRUE); microbenchmark(f5(x), f6(x), f8(x), f9(x), times=10)
Unit: milliseconds
expr min lq median uq max neval
f5(x) 291.8189 292.69771 293.2349 293.91216 296.3622 10
f6(x) 96.5717 96.73662 96.8249 99.25542 150.1081 10
f8(x) 659.3281 663.85092 669.6831 672.43613 699.4790 10
f9(x) 284.2978 296.41822 301.3535 331.92510 346.5567 10
> x <- sample(1:1e3, size=1e7, TRUE); microbenchmark(f5(x), f6(x), f8(x), f9(x), times=10)
Unit: milliseconds
expr min lq median uq max neval
f5(x) 3190.2555 3224.4201 3264.415 3359.823 3464.782 10
f6(x) 980.1287 989.9998 1051.559 1056.484 1085.580 10
f8(x) 5092.5847 5142.3289 5167.101 5244.400 5348.513 10
f9(x) 2799.6125 2843.1189 2881.734 2977.116 3081.437 10
data.table
是赢家! - 至今 :-)
f8
来允许类似
c(5,2,2,10)
的输入,其中并不存在从
1
到
max(x)
的所有整数。
最佳答案
这比tabulate
慢一点,但是更通用(它可以处理字符,因素,基本上不管您扔给它什么),并且更容易阅读/维护/扩展。
library(data.table)
f6 = function(x) {
data.table(x)[, .N, keyby = x]
}
x <- sample(1:1000, size=1e7, TRUE)
system.time(f6(x))
# user system elapsed
# 0.80 0.07 0.86
system.time(f8(x)) # tabulate + dickoa's conversion to data.frame
# user system elapsed
# 0.56 0.04 0.60
data.table
版本1.9.3开始,
data.table
版本实际上比
tabulate
+
data.frame
转换快大约2倍。
关于performance - 计算每个唯一元素出现次数的最快方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17223308/
我想了解 Ruby 方法 methods() 是如何工作的。 我尝试使用“ruby 方法”在 Google 上搜索,但这不是我需要的。 我也看过 ruby-doc.org,但我没有找到这种方法。
Test 方法 对指定的字符串执行一个正则表达式搜索,并返回一个 Boolean 值指示是否找到匹配的模式。 object.Test(string) 参数 object 必选项。总是一个
Replace 方法 替换在正则表达式查找中找到的文本。 object.Replace(string1, string2) 参数 object 必选项。总是一个 RegExp 对象的名称。
Raise 方法 生成运行时错误 object.Raise(number, source, description, helpfile, helpcontext) 参数 object 应为
Execute 方法 对指定的字符串执行正则表达式搜索。 object.Execute(string) 参数 object 必选项。总是一个 RegExp 对象的名称。 string
Clear 方法 清除 Err 对象的所有属性设置。 object.Clear object 应为 Err 对象的名称。 说明 在错误处理后,使用 Clear 显式地清除 Err 对象。此
CopyFile 方法 将一个或多个文件从某位置复制到另一位置。 object.CopyFile source, destination[, overwrite] 参数 object 必选
Copy 方法 将指定的文件或文件夹从某位置复制到另一位置。 object.Copy destination[, overwrite] 参数 object 必选项。应为 File 或 F
Close 方法 关闭打开的 TextStream 文件。 object.Close object 应为 TextStream 对象的名称。 说明 下面例子举例说明如何使用 Close 方
BuildPath 方法 向现有路径后添加名称。 object.BuildPath(path, name) 参数 object 必选项。应为 FileSystemObject 对象的名称
GetFolder 方法 返回与指定的路径中某文件夹相应的 Folder 对象。 object.GetFolder(folderspec) 参数 object 必选项。应为 FileSy
GetFileName 方法 返回指定路径(不是指定驱动器路径部分)的最后一个文件或文件夹。 object.GetFileName(pathspec) 参数 object 必选项。应为
GetFile 方法 返回与指定路径中某文件相应的 File 对象。 object.GetFile(filespec) 参数 object 必选项。应为 FileSystemObject
GetExtensionName 方法 返回字符串,该字符串包含路径最后一个组成部分的扩展名。 object.GetExtensionName(path) 参数 object 必选项。应
GetDriveName 方法 返回包含指定路径中驱动器名的字符串。 object.GetDriveName(path) 参数 object 必选项。应为 FileSystemObjec
GetDrive 方法 返回与指定的路径中驱动器相对应的 Drive 对象。 object.GetDrive drivespec 参数 object 必选项。应为 FileSystemO
GetBaseName 方法 返回字符串,其中包含文件的基本名 (不带扩展名), 或者提供的路径说明中的文件夹。 object.GetBaseName(path) 参数 object 必
GetAbsolutePathName 方法 从提供的指定路径中返回完整且含义明确的路径。 object.GetAbsolutePathName(pathspec) 参数 object
FolderExists 方法 如果指定的文件夹存在,则返回 True;否则返回 False。 object.FolderExists(folderspec) 参数 object 必选项
FileExists 方法 如果指定的文件存在返回 True;否则返回 False。 object.FileExists(filespec) 参数 object 必选项。应为 FileS
我是一名优秀的程序员,十分优秀!