Here is one way. You can change depending on what you want the offsets to be: > s4 <- seq(length=10, from=1, by=5) > s4 [1] 1 6 11 16 21 26 31 36 41 46 > f.x <- function(vec, n) c(rep(0,n), vec)[1:length(vec)] > f.x(s4,2) [1] 0 0 1 6 11 16 21 26 31 36 > df <- data.frame(s4=s4, s4.1=f.x(s4,2), s4.2=f.x(s4,4)) > df s4 s4.1 s4.2 1 1 0 0 2 6 0 0 3 11 1 0 4 16 6 0 5 21 11 1 6 26 16 6 7 31 21 11 8 36 26 16 9 41 31 21 10 46 36 26 > df$sum <- rowSums(df) > df s4 s4.1 s4.2 sum 1 1 0 0 1 2 6 0 0 6 3 11 1 0 12 4 16 6 0 22 5 21 11 1 33 6 26 16 6 48 7 31 21 11 63 8 36 26 16 78 9 41 31 21 93 10 46 36 26 108 >
On 12/22/05, Rhett Eckstein <[EMAIL PROTECTED]> wrote: > > Dear R users: > > > s4 <- seq(length=10, from=1, by=5) > > s<-data.frame(s4,s4,s4) > I would like to do some modification to s. > And I want the form like the following,if it is possible, how should I do? > The last column is the sum of previous three column. > s4 s4.1 s4.2 sum > 1 1 1 > 2 6 6 > 3 11 1 12 > 4 16 6 22 > 5 21 11 1 33 > 6 26 16 6 48 > 7 31 21 11 63 > 8 36 26 16 78 > 9 41 31 21 93 > 10 46 36 26 108 > > > Thanks for any help !! > > ______________________________________________ > [email protected] mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html > -- Jim Holtman Cincinnati, OH +1 513 247 0281 What the problem you are trying to solve? [[alternative HTML version deleted]] ______________________________________________ [email protected] mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
