Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot create an empty vector and append new elements in R

Tags:

r

I am just beginning to learn R and am having an issue that is leaving me fairly confused. My goal is to create an empty vector and append elements to it. Seems easy enough, but solutions that I have seen on stackoverflow don't seem to be working.

To wit,

>     a <- numeric() >     append(a,1) [1] 1 >     a numeric(0) 

I can't quite figure out what I'm doing wrong. Anyone want to help a newbie?

like image 477
user1935935 Avatar asked Jan 18 '13 03:01

user1935935


People also ask

Can you append to an empty vector in R?

We can insert all types of vectors in one empty vector.

How do I create an empty list and add values in R?

To create an empty list in R, use the vector() function. The vector() function takes two arguments: mode and length. The mode is, in our case, is a list, and length is the number of elements in the list, and the list ends up actually empty, filled with NULL.

How do I append to a vector in R?

To append elements to a Vector in R, use the append() method. The append() is a built-in method that adds the various integer values into a Vector in the last position.


1 Answers

append does something that is somewhat different from what you are thinking. See ?append.

In particular, note that append does not modify its argument. It returns the result.

You want the function c:

> a <- numeric() > a <- c(a, 1) > a [1] 1 
like image 84
Matthew Lundberg Avatar answered Oct 05 '22 22:10

Matthew Lundberg