bex
2014-Feb-25 20:09 UTC
[R] R: Re: how to extract consecutive subvetors of different lengths from an existing vector and create a matrix
Thank you Arun!
Le mail ti raggiungono ovunque con BlackBerry® from Vodafone!
-----Original Message-----
From: "arun kirshna [via R]"
<ml-node+s789695n4685806h49@n4.nabble.com>
Date: Tue, 25 Feb 2014 10:54:10
To: bex<elisabetta.petracci@gmail.com>
Subject: Re: how to extract consecutive subvetors of different lengths from
an existing vector and create a matrix
Hi,
One way would be:
res <- t(sapply(split(y,rep(seq_along(n),n)),function(x) {if(length(x) <
max(n)) c(x,rep(NA,max(n)-length(x))) else x}))
A.K.
Dear all,
my question regards how to subset an existing vector in
subvectors of different lengths and then create a matrix. See example
below.
I have vectors:
y<-c(1,1,1,0,0,1,1)
n<-c(3,2,2)
and I'd like to create the matrix A of the following form:
1 1 1
0 0 NA
1 1 NA
I'd like to "cut" y sequentially based on information in vector
n, that is, generating 3 subvectors (the first with the first 3 elements
of y, the second with the other 2 elements of y, and the last one with
the latter 2 elements of y) and creating a matrix A (with dimensions,
no.of rows=length(n), no. of col=max(n) and including NAs where
necessary.
Hope to have been clear.
Bex
______________________________________________
R-help@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.
_______________________________________________
If you reply to this email, your message will be added to the discussion below:
http://r.789695.n4.nabble.com/how-to-extract-consecutive-subvetors-of-different-lengths-from-an-existing-vector-and-create-a-matrix-tp4685783p4685806.html
To unsubscribe from how to extract consecutive subvetors of different lengths
from an existing vector and create a matrix, visit
http://r.789695.n4.nabble.com/template/NamlServlet.jtp?macro=unsubscribe_by_code&node=4685783&code=ZWxpc2FiZXR0YS5wZXRyYWNjaUBnbWFpbC5jb218NDY4NTc4M3w4ODEyMDMyNDg
--
View this message in context:
http://r.789695.n4.nabble.com/R-Re-how-to-extract-consecutive-subvetors-of-different-lengths-from-an-existing-vector-and-create-a-x-tp4685820.html
Sent from the R help mailing list archive at Nabble.com.
[[alternative HTML version deleted]]