我如何从 R 中的聚合函数解释这个错误 [英] How do I interpret this error from aggregate function in R
问题描述
我需要一些有关聚合函数的帮助.这是我的数据:(它显示了三列,但我的理解是有两列,分别命名为 V1 和 V3.显示从 330 开始的数字的第一列是 row.names.)
I need some help with the aggregate function. Here is my data: (it shows three columns however my understanding is that there is 2 columns, named V1 and V3 respectively. The first column showing numbers starting at 330 are the row.names.)
我想在 V3 上使用 V1 作为键做一个简单的聚合函数,比如 mean.我感觉我的数据类型不正确,但我不知道如何转换!
I'd like to do a simple aggregate function like mean on V3 using V1 as the key. I have a feeling my data type is not correct, but I don't know how to convert!
> testing
V1 V3
330 chr1_10440000_11000000 1.59987556934357
335 chr1_10440000_11000000 89.185531616211
338 chr1_10440000_11000000 1.25018584728241
340 chr1_10440000_11000000 5.91385841369629
342 chr1_10440000_11000000 1.68633282184601
345 chr1_11000000_11240000 0.118176721036434
349 chr1_11000000_11240000 9.131010055542
350 chr1_11000000_11240000 0.0575727485120296
352 chr1_11000000_11240000 11.7410087585449
353 chr1_11000000_11240000 10.5057544708252
356 chr1_11000000_11240000 2.35379362106323
360 chr1_11240000_12040000 0.08041662722826
363 chr1_11240000_12040000 1.62903010845184
366 chr1_11240000_12040000 0.039043802767992
368 chr1_11240000_12040000 1.90981948375702
369 chr1_11240000_12040000 7.19360542297363
370 chr1_11240000_12040000 5.95961284637451
371 chr1_11240000_12040000 4.40743684768677
372 chr1_11240000_12040000 0.600234627723694
373 chr1_11240000_12040000 20.9832191467285
>
这是我尝试使用的代码和相应的错误/警告消息:
Here is the code I am trying to use and the corresponding error/warning message:
> aggregate(testing, by=list(testing$V1), FUN=mean )
Group.1 V1 V3
1 chr1_10440000_11000000 NA NA
2 chr1_11000000_11240000 NA NA
3 chr1_11240000_12040000 NA NA
Warning messages:
1: In mean.default(X[[1L]], ...) :
argument is not numeric or logical: returning NA
2: In mean.default(X[[2L]], ...) :
argument is not numeric or logical: returning NA
3: In mean.default(X[[3L]], ...) :
argument is not numeric or logical: returning NA
4: In mean.default(X[[1L]], ...) :
argument is not numeric or logical: returning NA
5: In mean.default(X[[2L]], ...) :
argument is not numeric or logical: returning NA
6: In mean.default(X[[3L]], ...) :
argument is not numeric or logical: returning NA
>
推荐答案
aggregate(V3 ~ V1, testing, FUN = function(x) mean(as.numeric(as.character(x))))
应该可以解决问题.在应用函数之前,变量被转换为数字变量.
should do the trick. The variable is transformed to a numeric one before applying the function.
这篇关于我如何从 R 中的聚合函数解释这个错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!