[R] extractor rows from a matrix

Petr Klasterecky klaster at karlin.mff.cuni.cz
Thu Jun 14 11:26:03 CEST 2007


Well, you cannot have a vector of vectors, you need a list of vectors. 
With mean() and sum() it worked probably because these produce a single 
number as their output. Also, the rbind() function won't help here, that 
is for merging vector into a matrix. Once again, reading R-Intro is the 
best advice I can give you.

# creating an ampty list of length 1096:
d <- list()
length(d) <- 1096

# Filling the list with rows
for(i in (1:1096)){
d[[i]] <- ht[i,]
}

Note the double bracketing when indexing lists: d[[1]]

Please respond to the entire list next time, I only read these messages 
occasionally.

Petr


vincenzo napsal(a):
> thank you a lot for the answer
>  
> i don't expect one vector..
> but 1096 vector.
>  
> example: ht=
>            1        2        3
> 1          3        4        5
> 2          4        5        8
> 3
> 4
> .
> .
> .
> 1096 
>  
> i'd like 1096 vector like these:
> d[1]= 3    4    5
> d[2]= 4    5    8
> ...
>  
> i'm trying now with this:
> for(i in 1:1096) d[i]=(rbind(ht[i,])) 
> but it doesn't work :(
> it works with function like "mean","sum".. but not with "rbind"
> what can i do?
>  
>  
>  
> /-------Messaggio originale-------/
>  
> /*Da:*/ Petr Klasterecky <mailto:klaster at karlin.mff.cuni.cz>
> /*Data:*/ 06/14/07 10:07:33
> /*A:*/ billycorg <mailto:billycorg1 at virgilio.it>
> /*Cc:*/ r-help at stat.math.ethz.ch <mailto:r-help at stat.math.ethz.ch>
> /*Oggetto:*/ Re: [R] extractor rows from a matrix
>  
> billycorg napsal(a):
>  > thanks for the answer..but i don't find what i'm looking for!
>  >
>  > now i'm trying to expose better my problem:
>  > i have:
>  >
>  > ht= a 1096rows x 3 columns matrix
>  >
>  > i'd like a function like this:
>  >
>  > d[i]=rbind(ht[i,]) for (i in 1:length(ht))
>  >
>  > but this don't work :(
>  > can anyone seriously help  me?
>  
> The problem is that people actually are trying to seriously help you,
> but it is really difficult from your queries...
>  
> Please specify what you expect to be your output - a vector where you
> just paste the rows of of the original matrix one by one?
> If so, you can do
> d <- t(ht)
> dim(d) <- NULL
>  
> or (a 'dirty' way)
> d <- c(t(ht))
>  
> It is not at all clear what your statement should do. For example, what
> is length(ht) with ht being a matrix? For R it is the total number of
> elements, but did you mean this or the number of rows/columns??
>  
> The suggestion to read R-Intro is the best advice you got here.
>  
> Petr
>  
> --
> Petr Klasterecky
> Dept. of Probability and Statistics
> Charles University in Prague
> Czech Republic
>  
> __________ Informazione NOD32 2328 (20070613) __________
>  
> Questo messaggio  è stato controllato dal Sistema Antivirus NOD32
> http://www.nod32.it <http://www.nod32.it/>
>  
>  
> 		
> 

-- 
Petr Klasterecky
Dept. of Probability and Statistics
Charles University in Prague
Czech Republic



More information about the R-help mailing list