Hey guys, I've search a few threads about deleting a value from a vector, but no one has addressed this question so far. I want to delete the last value from a string of values I have: r = [ 1, 2, 3, 4, 5 ], and i want to make r2 = to [ 1, 2, 3, 4] So that r2 is just like r, except that it missing the final value. Thanks, -- View this message in context: http://r.789695.n4.nabble.com/Deleting-the-last-value-of-a-vector-tp3456363p3456363.html Sent from the R help mailing list archive at Nabble.com.
if you have a vector like as follows; r=c(1,2,3,4,5) then use r2=r[1:length(r)-1] -- View this message in context: http://r.789695.n4.nabble.com/Deleting-the-last-value-of-a-vector-tp3456363p3456388.html Sent from the R help mailing list archive at Nabble.com.
Hi, Try this, snip = function(x, n=1) { rand = sample(1:3, 1) print(paste("using algorithm #", rand)) switch(rand, '1' = head(x, length(x) - n), '2' = x[ seq(1, length(x) - n) ], '3' = x[ - seq(length(x), by=-1, length=n) ]) } snip(1:5) HTH, but please do read the posting guide. baptiste On 18 April 2011 12:51, empyrean <ctross at ucdavis.edu> wrote:> Hey guys, > > I've search a few threads about deleting a value from a vector, but no one > has addressed this question so far. > > I want to delete the last value from a string of values > > I have: > > r = [ 1, 2, 3, 4, 5 ], and i want to make r2 = to [ 1, 2, 3, 4] > > So that r2 is just like r, except that it missing the final value. > > Thanks, > > -- > View this message in context: http://r.789695.n4.nabble.com/Deleting-the-last-value-of-a-vector-tp3456363p3456363.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at 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. >
A easier solution: r <- c(1, 2, 3, 4, 5) r2<-r[1:length(r)-1] On Mon, Apr 18, 2011 at 10:51 AM, empyrean <ctross at ucdavis.edu> wrote:> Hey guys, > > I've search a few threads about deleting a value from a vector, but no one > has addressed this question so far. > > I want to delete the last value from a string of values > > I have: > > r = [ 1, 2, 3, 4, 5 ], and i want to make r2 = to [ 1, 2, 3, 4] > > So that r2 is just like r, except that it missing the final value. > > Thanks, > > -- > View this message in context: http://r.789695.n4.nabble.com/Deleting-the-last-value-of-a-vector-tp3456363p3456363.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at 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. >-- Daisy Englert Duursma Department of Biological Sciences Room E8C156 Macquarie University, North Ryde, NSW 210 Australia Tel +61 2 9850 9256
On Mon, Apr 18, 2011 at 4:14 AM, helin_susam <helin.susam at gmail.com> wrote:> if you have a vector like as follows; > > r=c(1,2,3,4,5) > > then use > > r2=r[1:length(r)-1]Umm ... this works and gives the intended answer but does so in an ugly way -- 1:length(r)-1 is equivalent to (1:length(r))-1 or 0:(length(r)-1) -- in other words, you get a sequence from 0 to 4. The result of r[0] is, curiously, integer(0) (but if you think about it, it might just as well be an error), so you get the intended result but what you probably actually meant is r2=r[1:(length(r)-1)] # or r2=r[1:seq_len(length(r)-1)] KK