And when I initialize the new array, would I need every value in this field to be initialized with an actual numerical value?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
How you append the new field will depend on the object you have. Options are given below. If the
length()of the variable you are trying to append does not equal the number of rows of the object you wish to append to then you will need to fill the new column/variable first withNAand then append your variable into the correct elements of the newly created object. For example, withand we want to add a vector
c(1,3,4)into rows 1, 2, and 5 of a third variable indf, then we could do this like so:Then using subsetting to insert the new data into the correct elements
which gives:
If the variable you wish to append is of the correct length, then you don’t need to initialise anything. Use one of the approaches described below to achieve what you want in that case, depending on whether you have a data frame or a matrix.
Assuming a data frame
If you have a data frame then there are several ways. For example, using
We can do
I prefer
transform()or the related
within()and you can also do
or
but the latter one doesn’t produce a variable with the name
"var3"but instead gets a default name.You can also use
cbind()to bind a column to a data frame (also works for matrices). Note that you have to either pass a data frame as the first argument or call the"data.frame"method directly otherwise you don’t get a data frame as the result:Assuming a matrix
If you have a matrix, then the options are reduced essentially to using
cbind(). Usingyou can do
The other options don’t work.