HI Eliza,
I looked into your data.
It's not a list of matrices, but a "list of data.frames" and as
suspected, some of the columns were "chr" instead of "num".
str(d) #only selected list elements which showed the anomaly
$ :'data.frame':??? 1998 obs. of? 13 variables:
? ..$ Col0 : num [1:1998] 1 2 3 4 5 6 7 8 9 10 ...
? ..$ Col1 : num [1:1998] 396 396 396 396 396 ...
? ..$ Col2 : num [1:1998] 379 371 371 371 371 ...
? ..$ Col3 : chr [1:1998] "353.75" "354.0" "345.0"
"345.0" ...
? ..$ Col4 : num [1:1998] 354 354 362 362 362 ...
? ..$ Col5 : num [1:1998] 492 447 561 527 527 ...
? ..$ Col6 : chr [1:1998] "1154.64" "1334.0"
"1002.0" "849.0" ...
? ..$ Col7 : num [1:1998] 3283 2888 2648 2662 2803 ...
? ..$ Col8 : num [1:1998] 5603 5663 5607 5578 5635 ...
? ..$ Col9 : num [1:1998] 3594 3313 2973 2662 2662 ...
? ..$ Col10: num [1:1998] 1053 1019 1002 951 917 ...
? ..$ Col11: num [1:1998] 515 501 493 481 479 470 462 459 447 447 ...
? ..$ Col12: num [1:1998] 405 402 396 396 388 379 379 377 371 365 ...
?$ :'data.frame':??? 1998 obs. of? 13 variables:
? ..$ Col0 : num [1:1998] 1 2 3 4 5 6 7 8 9 10 ...
? ..$ Col1 : num [1:1998] -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 ...
? ..$ Col2 : num [1:1998] -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 ...
? ..$ Col3 : chr [1:1998] "-1.0" "-1.0" "-1.0"
"-1.0" ...
? ..$ Col4 : num [1:1998] -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 ...
? ..$ Col5 : chr [1:1998] "-1.0" "-1.0" "-1.0"
"-1.0" ...
? ..$ Col6 : chr [1:1998] "-1.0" "-1.0" "-1.0"
"-1.0" ...
? ..$ Col7 : chr [1:1998] "-1.0" "-1.0" "-1.0"
"-1.0" ...
? ..$ Col8 : num [1:1998] -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 ...
? ..$ Col9 : num [1:1998] 6311 6452 6396 6169 5717 ...
? ..$ Col10: num [1:1998] 1695 1610 1568 1506 1407 ...
? ..$ Col11: num [1:1998] 645 640 634 628 617 ...
? ..$ Col12: num [1:1998] 470 464 461 456 453 ...
?
?$ :'data.frame':??? 1998 obs. of? 13 variables:
? ..$ Col0 : num [1:1998] 1 2 3 4 5 6 7 8 9 10 ...
? ..$ Col1 : num [1:1998] 29.4 29.4 28.9 29.7 30.6 ...
? ..$ Col2 : num [1:1998] 29.1 29.4 28.9 28 28 27.5 27.5 27.5 27.5 27.5 ...
? ..$ Col3 : num [1:1998] 30.6 31.1 30.3 30.3 31.1 30.3 30.6 30.6 29.7 31.1 ...
? ..$ Col4 : num [1:1998] 27.1 26.6 24.8 22.1 26.6 26.6 26.6 26.6 26.6 26.6 ...
? ..$ Col5 : num [1:1998] 45.3 46.4 46.4 47.5 50.4 51.5 53.2 53.2 54.3 55.5 ...
? ..$ Col6 : num [1:1998] 137 145 150 161 168 172 180 183 187 220 ...
? ..$ Col7 : chr [1:1998] "225.834" "211.684"
"194.704" "186.78" ...
? ..$ Col8 : num [1:1998] 337 504 583 405 376 ...
? ..$ Col9 : num [1:1998] 388 359 359 357 334 ...
? ..$ Col10: num [1:1998] 129 126 122 111 109 103 100 97.1 93.7 91.1 ...
? ..$ Col11: num [1:1998] 43.6 38.5 40.2 47.5 44.1 41.9 39.1 38.5 36.8 36.2 ...
? ..$ Col12: num [1:1998] 28.3 27.1 26.1 26.6 26.6 25.7 25.2 26.6 26.6 26.1 ...
When I checked the data more closely in sheet 11 (11, 12, 14 with anomaly),
found that in those chr columns, some numbers are followed by space and then
again 1 in the same cell.? For eg. (326.5 1).
d1<-lapply(d,function(x)
{do.call(data.frame,ifelse(grepl("\\d+\\s+\\d+",x),as.data.frame(apply(x,2,function(x)
as.numeric(gsub("[ ]","",x)))), x))})
d2<-lapply(d1,function(x) {names(x)<-
paste("Col",0:12,sep=""); x})
?#lapply(d2,function(x) sapply(x,is.numeric)) #can check whether all the columns
of list elements are numeric.
# str(d2)
e<-lapply(seq_along(d2), function(i)
{d2[[i]][apply(d2[[i]],1,function(x)any(!is.na(x))),]})
f<-lapply(seq_along(e), function(i)
{e[[i]][apply(e[[i]],2,function(x)any(!is.na(x))),]}) #not sure why you need
these two steps. Instead
#f1<-lapply(d2,na.omit)
r<- lapply(f, function(x){replace(x, x == -1, NA)})
# r1<- lapply(f1, function(x){replace(x, x == -1, NA)})
?sr<-lapply(r,function(x) colMeans(x,na.rm=TRUE))
#sr1<-lapply(r1,function(x) colMeans(x,na.rm=TRUE))
names(sr)<-paste("sr",1:16,sep="")
#names(sr1)<-paste("sr",1:16,sep="")
res<-do.call(rbind,sr)
head(res,3)
#???????? Col0????? Col1????? Col2????? Col3????? Col4????? Col5????? Col6
#sr1? 21.53977 445.82619 440.41702 519.98165 859.25831 2186.7239 5164.3338
#sr2? 75.75148? 38.60398? 48.15517 103.37507 200.48586? 290.2050? 427.8965
#sr3 617.59607? 76.16458? 68.22891? 67.89351? 92.70717? 190.4647? 489.7183
?#??????? Col7????? Col8????? Col9????? Col10???? Col11???? Col12
#sr1 7419.6656 6497.9376 2947.0133 1096.80798 678.91826 527.60351
#sr2? 426.5273? 297.5692? 137.7833?? 72.22109? 49.69003? 43.67117
#sr3? 800.9860? 748.0768? 371.0196? 162.00639 110.73111? 89.26638
?tail(res,2)
#????????? Col0????? Col1????? Col2???? Col3????? Col4???? Col5????? Col6
#sr15 615.98058? 53.62532? 49.89996 43.91249? 44.45947? 96.7349? 462.8477
#sr16? 21.53931 114.94487 102.43655 94.68324 126.93070 407.4416 1336.4197
#???????? Col7???? Col8???? Col9??? Col10??? Col11???? Col12
#sr15 1258.418 1306.892 520.8200 157.4494? 92.5196? 67.39349
#sr16 2114.459 1855.358 859.8711 318.6484 183.9742 138.65578
?
#res1<-do.call(rbind,sr1) #results are a bit different
?#head(res1,2)
? # ???? Col0???? Col1????? Col2???? Col3???? Col4????? Col5???? Col6????? Col7
#sr1 15.72368 458.3062 440.41702 518.4893 853.6490 2169.3466 5144.764 7423.5169
#sr2 15.91228? 39.8461? 48.15517 103.2362 200.2069? 289.7981? 427.628? 426.8419
? # ????? Col8???? Col9????? Col10???? Col11???? Col12
#sr1 6541.7739 2964.951 1101.95443 679.90060 527.97005
#sr2? 298.3639? 137.985?? 72.27442? 49.71482? 43.71521>
????????????????????????????????????????
Hope this helps.
A.K.
----- Original Message -----
From: eliza botto <eliza_botto at hotmail.com>
To: "r-help at r-project.org" <r-help at r-project.org>
Cc:
Sent: Wednesday, January 2, 2013 5:16 PM
Subject: [R] list of matrices
dear useRs,
i have a list containing 16 matrices. i want to calculate? the column mean of
each of them.
i tried>sr <- lapply(s,function(x) colMeans(x, na.rm=TRUE))
but i am getting the following error>Error in colMeans(x, na.rm = TRUE) : 'x' must be numeric
can it be done in any other way? and why i am getting this error??
thanks in advance..
elisa ??? ??? ??? ? ??? ??? ?
??? [[alternative HTML version deleted]]
______________________________________________
R-help at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.