[R] Problem with indexing

Uwe Ligges ligges at statistik.uni-dortmund.de
Thu Sep 12 13:15:10 CEST 2002


Bayesianbay at aol.com wrote:
> 
> Dear List
> 
> I am having a bit of a problem getting a program to work.
> 
> For each of i=1 to n persons I have a matrix (different for each person) with
> m rows.
> 
> What I want to do, is create m new data sets such that the first is made up
> of the first row for each person from the original matrices, the second
> contains the second row for each person from the original matrices etc etc up
> to the mth new set containing the mth row from the original matrices for each
> person.
> 
> Say the original matrix for each person can be indexed by xfinal.list[[i]]
> 
> I want to extract for each person in turn, the jth row, i.e.
> xfinal.list[[i]][j,] then bind these n vectors into a new matrix.
> 
> I am a little confused as to how I can index the jth row from the ith person,
> obviously I will need a loop
> 
> I thought I would be able to use something like:
> 
> for (j in 1:m) {
> for (i in 1:n) {
> vector.list[[i,j]]<-xfinal.list[[i]][j,]
> } }

Try:

  new.list <- vector(mode="list", length=m)
  for(j in 1:m){
    temp <- unlist(lapply(xfinal.list, function(x) x[j,])
    new.list[[j]] <- matrix(unlist(temp), nrow=n, byrow=TRUE)
  }
    

> However I get the error that [[]] is an improper number of subscripts
> 
> Could anyone tell me how I can index it so that I can call up say the jth row
> for the ith person?
> 
> As a secondary question, when using
> do.call("rbind", args) is it possible to use indexing i.e do.call("rbind",
> vector.list[[i,j]]) so that the vector from row j for each person are
> combined into a new matrix?

The double brackets are for list indexing - and that's one dimensional.
Two indexes are working for matrices/arrays in single brackets.

The do.call() explaination is left for its expert, Bill Venables.

Uwe Ligges
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list