[R] Replace last element in a vector - elegant solution?

Rainer M Krug r.m.krug at gmail.com
Thu Jul 29 13:12:29 CEST 2010


-----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-----



More information about the R-help mailing list