[R] Create vectors from a vector

David Winsemius dwinsemius at comcast.net
Sun Mar 14 17:45:50 CET 2010


On Mar 14, 2010, at 8:11 AM, Dimitris Kapetanakis wrote:

>
> Dear all,
>
> I would like to create a number of vectors which contain the the  
> first n
> elements of an existing vector.
> For example I have the vectors vnk (200x1) and vro(200x1) and I want  
> to
> create 200 vectors that contain the 1st till the n_th element of the  
> vectors
> and thus be able to create a vector s with 200 elements which are the
> product of all these vectors:
>
> i.e s1<-vnk[1]%*%vro[1] +p^(length(vp[1]+1))
>    s2<-vnk[1:2]%*%vro[1:2]+p^(length(vp[1:2]+1))
>    s3<-vnk[1:3]%*%vro[1:3]+p^(length(vp[1:3]+1))
>    ...............................
>    ...............................
>    s200<-vnk[1:200]%*%vro[1:200]+p^(length(vp[1:200]+1))
>
>    s<-[s1 s2 ......s200]
>
> In the code I created I can calculate only the 200th element
>
> vnk<-t(nk^(200:0))
> vro<-ro^(0:200)
>
> s<-nz*e*(vnk%*%vp)+p^(length(vp))

Here is a guess, untested because you provided no sample data objects:

s <- sapply(1:200, function (x) { vnk[1:x]%*%vro[1:x] +  
p^(length(vp[1:x]+1)) } )

>
> if lapply wouldn't need functions but vector arguments it would do  
> the job
> perfectly but I don't know the respective command for vectors. If  
> you know
> please tell me.
>
> Any help would be highly appreciated
>
> Thanks a lot
>
> Dimitris
>
>


David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list