On Mon, 2006-06-26 at 20:46 -0500, [EMAIL PROTECTED] wrote: > in my previous post in which i asked about creating sequences > from two vectors of numbers, all suggestions worked. > > tradevectors<-mapply(seq,from=tempa,to=tempb) > > or > > tradevectors<-sapply(1:length(tempa),function(x) seq(tempa[x],tempb[x]) > > >both return a list with 3 components. > > the problem is that i want to take these 3 sequences and > > use them as the indices of two other vectors, X and Y and mutiply them ( > element by element ) > > > >so, i tried > > > >temp<-sapply(1:length(tradevectors),function(i)X[tradevectors[[i]]]*Y[tradevectors[[i]]] > > > basically, the sequence output from the previous command are > indices to two vectors that i want to multiply ( element > by element ). > > but, the message i get is that i cannot coerce list object to double. > > i looked up the info on unlist, but that just > makes long vector. i need 3 vectors.
How about something like this: tempa <- c(4, 6, 10) tempb <- c(11, 23, 39) tradevectors <- mapply(seq, from = tempa, to = tempb) > tradevectors [[1]] [1] 4 5 6 7 8 9 10 11 [[2]] [1] 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 [[3]] [1] 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 [23] 32 33 34 35 36 37 38 39 X <- seq(2, 80, 2) Y <- 1:40 > X [1] 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 [23] 46 48 50 52 54 56 58 60 62 64 66 68 70 72 74 76 78 80 > Y [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 [23] 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 > lapply(tradevectors, function(x) X[x] * Y[x]) [[1]] [1] 32 50 72 98 128 162 200 242 [[2]] [1] 72 98 128 162 200 242 288 338 392 450 512 578 648 [14] 722 800 882 968 1058 [[3]] [1] 200 242 288 338 392 450 512 578 648 722 800 882 968 [14] 1058 1152 1250 1352 1458 1568 1682 1800 1922 2048 2178 2312 2450 [27] 2592 2738 2888 3042 For example, the first value in [[1]] is 32, which is: X[4] * Y[4] # The '4' comes from tradevectors[[1]][1] which is: 8 * 4 The other list elements are similarly constructed. You can use lapply() when you want to apply functions to individual list elements (in this case vectors) as you traverse the list tree. See ?lapply HTH, Marc Schwartz ______________________________________________ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html