Table of Contents
What is the r append list function and what are its benefits?
For example, let’s say we have a list called friend which contains the names “Jane,” “John” and “Mary.” And then we have another list called family which contains the names “David,” “Peter,” and “Ann.” We want to combine them together using the r append function. First let’s create a new list called combined which is simply a concatenation of all the elements from both lists.
combined = r append friend , family
Then we display it:
> combined [1] “Jane David John Mary Ann”
we see that as expected, each element of friend was concatenated with every element of family to form a list with 7 elements. This is also true for the other way around, but with a different result:
> combined [1] “John Peter”

How do you use the r append list function in your own code?
Let’s say we want to append the name “Jenny” to the end of the friend list. We could use this function: r append list(add1=”Jenny”) , like so: r append list(add1=”Jenny”) friend name r append list(family) name r append list(family) name r append list(family) name r append list(family) name r append list(birthday) <-- the blank space is where Jenny's birthday goes
The first time we use add1, we get “Jenny”. Then it adds the birthday, “Foo”, and we get: r append list(add1=”Jenny” family birthday)
We can learn a lot this way. We can learn what’s inside of the big red button.
Learning to make new buttons
What are some of the potential problems with using the r append list function in your codebase?
But this time, not only do we get the error message “Append: could not find function add2,” but we have now overwritten the values of John and Mary in the friend list!
This can be a common problem when using the “r append” function in your codebase. When you use this function, it uses the list that is currently on the right hand side (RHS) of the assignment operator while creating the new list. This can result in unexpected behavior if you are not aware of how RHS lists are handled.
How can you prevent these potential problems from occurring in your own codebase?
If you want to append the name “Jenny” to the end of the friend list and only append it once, you could call that function like so: r append_list(friend, add1=”Jenny”) name.
However, what if you want to append “Jenny” to the end of the friend list, but not just once. You could call that function like so: r append_list(friend, add1=”Jenny”) name.
Now in this second example, “Jenny” will be appended to the end of the list twice.
Another example, which should make the previous two examples even more clear, is the following. Suppose you want to insert a new list in the friend list. Before inserting, you want to set up some variables that will keep track of where all elements go. Maybe they go into a list or maybe they are stored within the friend object itself. Either way, those variables should be local to the function that sets up this new list.
Here is what the function could look like: r append_list(friend, add1=”Jenny”) name.
These are only a few examples. The list-append functions may do something else. I am using these examples for illustration purposes only.

Are there any other ways to add items to a list in R that you should be aware of?
This time, not only do we get the error message “Append: could not find function add2,” but we have overwritten the values of John and Mary in the friend list! If you want to append the name “Jenny” to the end of the friend list and only append it once, you could call that function like so: r append_list(friend, add1=”Jenny”) , like so: r append_list(friend, add1=”Jenny”) friend name.name friend name.name
As you can see in the snippet above, I am just passing the call to “r append_list” before it is called to the object stored in “friend,” instead of storing a reference to such an object and then trying to store that in the function call. For example, we could instead write: r append_list(friend, add1=”Jenny”) friend name.name friend name.name friends <- get_friends() friends name.name friend name.name
One thing we should consider when using the get_friends() function is that it can not only find friends with the same name, but also friends of friends!
Since the friend list from my class is not very long, a quick way to see if Jenny would be added to it would be to just print out the friend list and check for her name.
