I am trying to join two lists in R, but I really don't know how to do it. I need a way to join the to lists by adding only the elements that have a different name from the ones to the first list, say:
list1=list('a'=1,'b'=2,'c'=3,'d'=4,'e'=5);
list2=list('b'=10,'c'=100,'e'=98);
list3=OpErAtIoN(list1,list2)
And one gets that list3 is list('a'=1,'b'=10,'c'=100,'d'=4,'e'=98)
Do you know any way to do it? Thank you!
R provided two inbuilt functions named c() and append() to combine two or more lists. c() function in R language accepts two or more lists as parameters and returns another list with the elements of both the lists. Example 1: R.
To append an element in the R List, use the append() function. You can use the concatenate approach to add components to a list. While concatenate does a great job of adding elements to the R list, the append() function operates faster.
To combine data frames stored in a list in R, we can use full_join function of dplyr package inside Reduce function.
The [[ operator can be used to extract single elements from a list. Here we extract the first element of the list. The [[ operator can also use named indices so that you don't have to remember the exact ordering of every element of the list. You can also use the $ operator to extract elements by name.
This gives the same result as your example:
modifyList(list1, list2)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With