Skip to content


R: removing the last elements of a vector the easy way

Every day I’m getting more fascinated by the weirdness of R. Sometimes it’s a bad surprise, but sometimes it’s a good one. After a crash in some time-consuming procedure, I still wanted to be able to plot my partial results properly. For this I had to remove some space I assigned at the beginning of the procedure (by creating result vectors filled with NAs). It turns out there’s a very quick and easy way to trim a vector to keep only the N first values:
length(yourVector)=N;

If you want to quickly try on a test vector:
myVec=1:50;
myVec;
length(myVec)=4;
myVec;

Posted in R (R-project).


0 Responses

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.



Some HTML is OK

or, reply to this post via trackback.

Sorry about the CAPTCHA that requires JS. If you really don't want to enable JS and still want to comment, you can send me your comment via e-mail and I'll post it for you.

Please solve the CAPTCHA below in order to fight spamWordPress CAPTCHA