Rainer M Krug
2010-Jul-29 11:12 UTC
[R] Replace last element in a vector - elegant solution?
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hi I have to replace the last element of a vector with 0, but the length of x is not known at coding time. So I do the following: x <- 1:10 x x[length(x)] <- 0 x it is working nicely, but I am wondering: is there a more elegant solution( like tail(x, 1) <- 0 , which does not work)? Thanks, Rainer - -- Rainer M. Krug, PhD (Conservation Ecology, SUN), MSc (Conservation Biology, UCT), Dipl. Phys. (Germany) Centre of Excellence for Invasion Biology Natural Sciences Building Office Suite 2039 Stellenbosch University Main Campus, Merriman Avenue Stellenbosch South Africa Tel: +33 - (0)9 53 10 27 44 Cell: +27 - (0)8 39 47 90 42 Fax (SA): +27 - (0)8 65 16 27 82 Fax (D) : +49 - (0)3 21 21 25 22 44 Fax (FR): +33 - (0)9 58 10 27 44 email: Rainer at krugs.de Skype: RMkrug -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.10 (GNU/Linux) Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/ iEYEARECAAYFAkxRYh0ACgkQoYgNqgF2ego4iQCePSFCnWMPuY1eD2fyXUUXGRmF PXgAnjIDIB0EnODWv4HuYjwgU3SJW01x =JVQu -----END PGP SIGNATURE-----
Henrique Dallazuanna
2010-Jul-29 12:53 UTC
[R] Replace last element in a vector - elegant solution?
Another way: x <- replace(x, length(x), 0) On Thu, Jul 29, 2010 at 8:12 AM, Rainer M Krug <r.m.krug@gmail.com> wrote:> -----BEGIN PGP SIGNED MESSAGE----- > Hash: SHA1 > > Hi > > I have to replace the last element of a vector with 0, but the length of > x is not known at coding time. So I do the following: > > x <- 1:10 > x > > x[length(x)] <- 0 > x > > it is working nicely, but I am wondering: is there a more elegant > solution( like tail(x, 1) <- 0 , which does not work)? > > Thanks, > > Rainer > - -- > Rainer M. Krug, PhD (Conservation Ecology, SUN), MSc (Conservation > Biology, UCT), Dipl. Phys. (Germany) > > Centre of Excellence for Invasion Biology > Natural Sciences Building > Office Suite 2039 > Stellenbosch University > Main Campus, Merriman Avenue > Stellenbosch > South Africa > > Tel: +33 - (0)9 53 10 27 44 > Cell: +27 - (0)8 39 47 90 42 > Fax (SA): +27 - (0)8 65 16 27 82 > Fax (D) : +49 - (0)3 21 21 25 22 44 > Fax (FR): +33 - (0)9 58 10 27 44 > email: Rainer@krugs.de > > Skype: RMkrug > -----BEGIN PGP SIGNATURE----- > Version: GnuPG v1.4.10 (GNU/Linux) > Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/ > > iEYEARECAAYFAkxRYh0ACgkQoYgNqgF2ego4iQCePSFCnWMPuY1eD2fyXUUXGRmF > PXgAnjIDIB0EnODWv4HuYjwgU3SJW01x > =JVQu > -----END PGP SIGNATURE----- > > ______________________________________________ > R-help@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. >-- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40" S 49° 16' 22" O [[alternative HTML version deleted]]
Hi, The function head also works. x <- 1:10 head(x,-1) ----- A R learner. -- View this message in context: http://r.789695.n4.nabble.com/Replace-last-element-in-a-vector-elegant-solution-tp2306315p2306471.html Sent from the R help mailing list archive at Nabble.com.