Quantcast

list of matrices

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

list of matrices

eliza botto

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]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: list of matrices

arun kirshna
Hi,
Check this link
http://tolstoy.newcastle.edu.au/R/help/05/04/1765.html

set.seed(15)
list1<-lapply(1:3,function(i) matrix(sample(1:10,6,replace=TRUE),ncol=i) )
lapply(list1,function(x) colMeans(x,na.rm=TRUE))
[[1]]
#[1] 6.666667

#[[2]]
#[1] 6.333333 6.000000
##
#[[3]]
#[1] 7.0 9.0 7.5
A.K.





----- Original Message -----
From: eliza botto <[hidden email]>
To: "[hidden email]" <[hidden email]>
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]]

______________________________________________
[hidden email] 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.


______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: list of matrices

Bert Gunter
In reply to this post by eliza botto
I fail to understand the purpose of your post. The error message seems
clear enough: either your matrices are not all numeric or you may have data
frames among them with non-numeric columns. Have you checked either or both?

?is.matrix
?is.numeric

as in
lapply(s,is.matrix(x) && is.numeric(x))

(with slight caveats with factors).

-- Bert


On Wed, Jan 2, 2013 at 2:16 PM, eliza botto <[hidden email]> wrote:

>
> 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]]
>
> ______________________________________________
> [hidden email] 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.
>



--

Bert Gunter
Genentech Nonclinical Biostatistics

Internal Contact Info:
Phone: 467-7374
Website:
http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: list of matrices

arun kirshna
In reply to this post by arun kirshna


HI Elisa,
The data that you sent to me is a matrix, (not the list containing 16 matrices).


 source("Elizanew.txt")
 is.matrix(dat1)
#[1] TRUE
dim(dat1)
#[1] 558  12
dat1<-na.omit(dat1)
 nrow(dat1)
#[1] 508
colMeans(dat1)
 #[1]   970.1942   892.5579   893.3180  1629.0733  5692.8488 12529.8313
 #[7] 13965.5763  9270.4147  4352.4091  2082.5329  1441.8123  1158.6669
A.K.

________________________________
From: eliza botto <[hidden email]>
To: "[hidden email]" <[hidden email]>
Sent: Wednesday, January 2, 2013 5:41 PM
Subject: RE: [R] list of matrices



dear arun,
can u please try your command on the following




elisa

> Date: Wed, 2 Jan 2013 14:34:05 -0800
> From: [hidden email]
> Subject: Re: [R] list of matrices
> To: [hidden email]
> CC: [hidden email]
>
> Hi,
> Check this link
> http://tolstoy.newcastle.edu.au/R/help/05/04/1765.html
>
> set.seed(15)
> list1<-lapply(1:3,function(i) matrix(sample(1:10,6,replace=TRUE),ncol=i) )
> lapply(list1,function(x) colMeans(x,na.rm=TRUE))
> [[1]]
> #[1] 6.666667
>
> #[[2]]
> #[1] 6.333333 6.000000
> ##
> #[[3]]
> #[1] 7.0 9.0 7.5
> A.K.
>
>
>
>
>
> ----- Original Message -----
> From: eliza botto <[hidden email]>
> To: "[hidden email]" <[hidden email]>
> 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]]
>
> ______________________________________________
> [hidden email] 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.
>

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: list of matrices

arun kirshna
In reply to this post by eliza botto
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 <[hidden email]>
To: "[hidden email]" <[hidden email]>
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]]

______________________________________________
[hidden email] 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.


______________________________________________
[hidden email] 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.
Loading...