> x <- c(1, 0, 6, 3, 8, 9, 4, 3, 0)
> m <- rbind(c(2,3,5), c(5,7,8), c(9,9,11))
> m
[,1] [,2] [,3]
[1,] 2 3 5
[2,] 5 7 8
[3,] 9 9 11> # create matrix of indices and values
> mI <- do.call('rbind', apply(m, 1, function(y)cbind(y[1]:y[2],
y[3])))
> mI
[,1] [,2]
[1,] 2 5
[2,] 3 5
[3,] 5 8
[4,] 6 8
[5,] 7 8
[6,] 9 11> # store new values
> x[mI[,1]] <- mI[,2]
> x
[1] 1 5 5 3 8 8 8 3 11>
>
On 3/25/06, Fred J. <phddas@yahoo.com> wrote:>
> dear R users
>
>
> I need to modify values in a vector, the modification needs to be based on
> a matrix which define the from_index, to_index and a value.
>
> example:
> > x <- c(1, 0, 6, 3, 8, 9, 4, 3, 0)
> > m
> from to value
> 1 2 3 5
> 2 5 7 8
> 3 9 9 11
> >
> expected:
> 1 5 5 3 8 8 8 3 11
> insert {R.utils} and append are too expensive and indirect for the task.
>
>
> thank you
>
>
>
> ---------------------------------
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> 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
>
--
Jim Holtman
Cincinnati, OH
+1 513 646 9390 (Cell)
+1 513 247 0281 (Home)
What the problem you are trying to solve?
[[alternative HTML version deleted]]