简体   繁体   English

我如何从 R 中的聚合函数解释这个错误

[英]How do I interpret this error from aggregate function in R

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.)这是我的数据:(它显示了三列,但我的理解是有两列,分别命名为 V1 和 V3。显示从 330 开始的数字的第一列是 row.names。)

I'd like to do a simple aggregate function like mean on V3 using V1 as the key.我想在 V3 上使用 V1 作为键做一个简单的聚合函数,比如 mean 。 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.在应用函数之前,变量被转换为数字变量。

I had the same problem with the aggregate() function, using mean() individually on each variable of the data frame was working fine (no error at all), however using mean inside the aggregate function was getting the warnings and returned NA data.我在使用aggregate() 函数时遇到了同样的问题,在数据框的每个变量上单独使用mean() 工作正常(根本没有错误),但是在聚合函数中使用mean 会收到警告并返回NA 数据。 solved with the proposed solution:用建议的解决方案解决了:

aggregate(.~Fecha, data=meteorologia, FUN = function(x) mean(as.numeric(as.character(x))))聚合(.~Fecha,数据=气象学,乐趣=函数(x)意思(as.numeric(as.character(x))))

running version运行版本

version _版本 _
platform x86_64-apple-darwin17.0平台 x86_64-apple-darwin17.0
arch x86_64拱形 x86_64
os darwin17.0操作系统达尔文17.0
system x86_64, darwin17.0系统 x86_64,darwin17.0
status地位
major 4大四
minor 0.2次要 0.2
year 2020 2020年
month 06 06月
day 22第 22 天
svn rev 78730 SVN 修订版 78730
language R语言R
version.string R version 4.0.2 (2020-06-22) nickname Taking Off Again version.string R version 4.0.2 (2020-06-22) 昵称再次起飞

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM