Reputation: 202
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.)
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
>
Upvotes: 0
Views: 5224
Reputation: 11
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. solved with the proposed solution:
aggregate(.~Fecha, data=meteorologia, FUN = function(x) mean(as.numeric(as.character(x))))
running version
version _
platform x86_64-apple-darwin17.0
arch x86_64
os darwin17.0
system x86_64, darwin17.0
status
major 4
minor 0.2
year 2020
month 06
day 22
svn rev 78730
language R
version.string R version 4.0.2 (2020-06-22) nickname Taking Off Again
Upvotes: 1
Reputation: 81693
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.
Upvotes: 5