- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我有一个包含 50000 行和 200 列的数据框。数据中有重复行,我想通过使用 R 中的聚合函数在重复项中选择具有最大变异系数的行来聚合数据。对于聚合,我可以默认使用“均值”、“总和”,但不能使用系数变异.
例如
aggregate(data, as.columnname, FUN=mean)
工作正常。
我有一个用于计算变异系数的自定义函数,但不确定如何将其与聚合一起使用。
co.var <- function(x)
(
100*sd(x)/mean(x)
)
我试过了
aggregate(data, as.columnname, function (x) max (co.var (x, data[index (x),])
但由于未找到对象 x 而给出错误。
最佳答案
假设我理解您的问题,我会建议使用 tapply()
而不是 aggregate()
(有关详细信息,请参阅 ?tapply
).但是,一个最小的工作示例将非常有帮助。
co.var <- function(x) ( 100*sd(x)/mean(x) )
## Data with multiple repeated measurements.
## There are three things (ID 1, 2, 3) that
## are measured two times, twice each (val1 and val2)
myDF<-data.frame(ID=c(1,2,3,1,2,3),val1=c(20,10,5,25,7,2),
val2=c(19,9,4,24,4,1))
## Calculate coefficient of variation for each measurement set
myDF$coVar<-apply(myDF[,c("val1","val2")],1,co.var)
## Use tapply() instead of aggregate
mySel<-tapply(seq_len(nrow(myDF)),myDF$ID,function(x){
curSub<-myDF[x,]
return(x[which(curSub$coVar==max(curSub$coVar))])
})
## The mySel vector is then the vector of rows that correspond to the
## maximum coefficient of variation for each ID
myDF[mySel,]
编辑:
有更快的方法,下面是其中一种。但是,对于 40000 x 100 的数据集,上述代码在我的机器上只用了 16 到 20 秒。
# Create a big dataset
myDF <- data.frame(val1 = c(20, 10, 5, 25, 7, 2),
val2 = c(19, 9, 4, 24, 4, 1))
myDF <- myDF[sample(seq_len(nrow(myDF)), 40000, replace = TRUE), ]
myDF <- cbind(myDF, rep(myDF, 49))
myDF$ID <- sample.int(nrow(myDF)/5, nrow(myDF), replace = TRUE)
# Define a new function to work (slightly) better with large datasets
co.var.df <- function(x) ( 100*apply(x,1,sd)/rowMeans(x) )
# Create two datasets to benchmark the two methods
# (A second method proved slower than the third, hence the naming)
myDF.firstMethod <- myDF
myDF.thirdMethod <- myDF
计时原法
startTime <- Sys.time()
myDF.firstMethod$coVar <- apply(myDF.firstMethod[,
grep("val", names(myDF.firstMethod))], 1, co.var)
mySel <- tapply(seq_len(nrow(myDF.firstMethod)),
myDF.firstMethod$ID, function(x) {
curSub <- myDF.firstMethod[x, ]
return(x[which(curSub$coVar == max(curSub$coVar))])
}, simplify = FALSE)
endTime <- Sys.time()
R> endTime-startTime
Time difference of 17.87806 secs
时间秒法
startTime3 <- Sys.time()
coVar3<-co.var.df(myDF.thirdMethod[,
grep("val",names(myDF.thirdMethod))])
mySel3 <- tapply(seq_along(coVar3),
myDF[, "ID"], function(x) {
return(x[which(coVar3[x] == max(coVar3[x]))])
}, simplify = FALSE)
endTime3 <- Sys.time()
R> endTime3-startTime3
Time difference of 2.024207 secs
检查我们是否得到相同的结果:
R> all.equal(mySel,mySel3)
[1] TRUE
原始帖子有一个额外的变化,编辑后的代码认为对于给定的 ID,可能有不止一行具有最高的 CV。因此,要从编辑的代码中获取结果,您必须unlist
mySel
或 mySel3
对象:
myDF.firstMethod[unlist(mySel),]
myDF.thirdMethod[unlist(mySel3),]
关于r - 在聚合中使用变异系数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9800088/
我已经训练了一个线性回归模型来以多输出方式进行预测。这是一个时间序列预测问题,根据一组输入来估计 future 12 个月的需求。在过去 - 如果我只预测一个输出值 - 我会简单地调用以下命令来访问模
我的应用程序对原始音频信号执行FFT(所有麦克风读数均为values中的16位整数值,即1024个单元)。它首先根据16位对读数进行归一化。然后提取频率为400Hz的幅度。 int sample_ra
我从包含相同语音内容的两个大约 30 秒的音频文件中提取了两个系列 MFCC 系数。音频文件从不同来源录制在同一位置。应该估计音频是否包含相同的对话或不同的对话。目前我已经测试了两个Mfcc系列的相关
我正在尝试使用两个自变量(批处理和方法)和一个因变量 (conc) 运行线性回归。当我运行回归时,我得到一个系数的 NA 值。当我更改模型中自变量的顺序时,NA 值显示为不同的变量。这是一个可重现的数
我想要的,大图:我想知道如何在 Java 中模仿 Mathematica 的 ExtendedGCD[...] 功能。有关该功能的信息可以在 here 中找到,但为了完整起见,我将对其进行简要描述。
我用 R 编写了这个套索代码,并且得到了一些 beta 值: #Lasso library(MASS) library(glmnet) Boston=na.omit(Boston) x=model.m
我有一个幅频响应点列表。振幅以分贝为单位给出。 任务是将此 AFR 列表导出为某些硬件 DSP 设备的一组系数。我知道此设备的文件格式。该设备的规范表明它需要 12288 个系数,并且 DSP 有 2
是否可以替换 lm 对象中的系数? 我认为以下会起作用 # sample data set.seed(2157010) x1 <- 1998:2011 x2 <- x1 + rnorm(length(
我正在尝试使用 lm.ridge 方法执行岭回归。我的问题是如何获得拟合模型的系数?我从调用中得到不同的结果: 模型$coef 系数(模型) 哪个是正确的?另外,为什么我从调用中得到不同的结果: co
以下代码片段死于 Eigen 断言: MatrixXd L; VectorXd x, b; ... ASSERT_MATRIX_EQ(L*x, b); 与, template void ASSERT
有没有办法从编码的图像和视频中轻松提取 DCT 系数(和量化参数)?任何解码器软件都必须使用它们来解码 block DCT 编码的图像和视频。所以我很确定解码器知道它们是什么。有没有办法将它们暴露给使
如何找到矢量 w 是多少,即垂直于分离平面? 最佳答案 我就是这样做的here .如果我没记错的话,这是基于 dual form 的SVM 优化的结果。 model = svmtrain(...);
我是 FFMPEG 的新手。我知道如何提取关键帧, 但现在我想提取那些 I 帧的 DC 系数。 您能否帮助编写 FFMPEG 命令的正确语法以获取 DC 系数? 问候 最佳答案 FFmpeg 命令不会
任何人都可以共享任何代码或可调用以从 HEVC 比特流中提取变换系数 (DCT/DST) 的函数吗? 我检查了 HM 16.0 的源代码。文件\source\Lib\TLibCommon\TComTr
我需要从 MPEG4 视频中提取有关运动矢量和 DC 系数的信息。我搜索了相关站点和主题,发现一个好的解决方案是使用 ffmpeg 编解码器的代码。尤其是 libavcodec/mpegvideo.c
我想删除sympy符号表达式中等于1的任何系数,例如:我希望 1.0x**2 成为 x**2 ,有办法做到吗?另外,如果可能的话,可以对整数进行舍入,例如将 2.0x**2 变为 2*x**2 最佳答
我想删除sympy符号表达式中等于1的任何系数,例如:我希望 1.0x**2 成为 x**2 ,有办法做到吗?另外,如果可能的话,可以对整数进行舍入,例如将 2.0x**2 变为 2*x**2 最佳答
我最近的任务是将 PMML 解析回 R 模型。 (我进行了广泛搜索,没有库可以为您进行这种转换。)我正在尝试将包含多项逻辑回归的 PMML 转换回 R 模型,但我不知道如何转换任何PMML 文档中保存
当使用 pandas statsmodels 的 .summary() 函数时,OLS 回归结果包括以下字段。 coef std err t P>|t| [
我正在 STM32F4 微 Controller 上使用 CMSIS 库开发 PID Controller 。我实在无法理解PID参数的归一化。现在我有了 PID Controller 的三个 Kp、
我是一名优秀的程序员,十分优秀!