rlist provides list.join
to perform simple joining of two lists, and list.merge
to perform modification merge of a series of lists.
devs <-
list(
p1=list(name="Ken",age=24,
interest=c("reading","music","movies"),
lang=list(r=2,csharp=4,python=3)),
p2=list(name="James",age=25,
interest=c("sports","music"),
lang=list(r=3,java=2,cpp=5)),
p3=list(name="Penny",age=24,
interest=c("movies","reading"),
lang=list(r=1,cpp=4,python=2)))
list.join
is used to join two lists by a key evaluated from either a common expression for the two lists or two separate expressions for each list.
newinfo <-
list(
p1=list(name="Ken",email="ken@xyz.com"),
p2=list(name="Penny",email="penny@xyz.com"),
p3=list(name="James",email="james@xyz.com"))
str(list.join(devs,newinfo,name))
# List of 3
# $ p1:List of 5
# ..$ name : chr "Ken"
# ..$ age : num 24
# ..$ interest: chr [1:3] "reading" "music" "movies"
# ..$ lang :List of 3
# .. ..$ r : num 2
# .. ..$ csharp: num 4
# .. ..$ python: num 3
# ..$ email : chr "ken@xyz.com"
# $ p2:List of 5
# ..$ name : chr "James"
# ..$ age : num 25
# ..$ interest: chr [1:2] "sports" "music"
# ..$ lang :List of 3
# .. ..$ r : num 3
# .. ..$ java: num 2
# .. ..$ cpp : num 5
# ..$ email : chr "james@xyz.com"
# $ p3:List of 5
# ..$ name : chr "Penny"
# ..$ age : num 24
# ..$ interest: chr [1:2] "movies" "reading"
# ..$ lang :List of 3
# .. ..$ r : num 1
# .. ..$ cpp : num 4
# .. ..$ python: num 2
# ..$ email : chr "penny@xyz.com"
list.merge
is used to recursively merge a series of lists with the later always updates the former. It works with two lists, as shown in the example below, in which a revision is merged with the original list.
rev1 <-
list(
p1=list(age=25),
p2=list(lang=list(r=2,cpp=4)),
p3=list(lang=list(r=2,python=NULL)))
str(list.merge(devs,rev1))
# List of 3
# $ p1:List of 4
# ..$ name : chr "Ken"
# ..$ age : num 25
# ..$ interest: chr [1:3] "reading" "music" "movies"
# ..$ lang :List of 3
# .. ..$ r : num 2
# .. ..$ csharp: num 4
# .. ..$ python: num 3
# $ p2:List of 4
# ..$ name : chr "James"
# ..$ age : num 25
# ..$ interest: chr [1:2] "sports" "music"
# ..$ lang :List of 3
# .. ..$ r : num 2
# .. ..$ java: num 2
# .. ..$ cpp : num 4
# $ p3:List of 4
# ..$ name : chr "Penny"
# ..$ age : num 24
# ..$ interest: chr [1:2] "movies" "reading"
# ..$ lang :List of 2
# .. ..$ r : num 2
# .. ..$ cpp: num 4
The function also works with multiple lists. When the second revision is obtained, the three lists can be merged in order.
rev2 <-
list(
p1=list(lang=list(csharp=5)),
p2=list(age=24,lang=list(r=3)))
str(list.merge(devs,rev1,rev2))
# List of 3
# $ p1:List of 4
# ..$ name : chr "Ken"
# ..$ age : num 25
# ..$ interest: chr [1:3] "reading" "music" "movies"
# ..$ lang :List of 3
# .. ..$ r : num 2
# .. ..$ csharp: num 5
# .. ..$ python: num 3
# $ p2:List of 4
# ..$ name : chr "James"
# ..$ age : num 24
# ..$ interest: chr [1:2] "sports" "music"
# ..$ lang :List of 3
# .. ..$ r : num 3
# .. ..$ java: num 2
# .. ..$ cpp : num 4
# $ p3:List of 4
# ..$ name : chr "Penny"
# ..$ age : num 24
# ..$ interest: chr [1:2] "movies" "reading"
# ..$ lang :List of 2
# .. ..$ r : num 2
# .. ..$ cpp: num 4