List updating is useful when we need to modify the original list but do not want to repeat the data.
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.update
partially modifies the given list by a number of lists resulted from expressions.
str(list.update(devs,age=age+1))
# 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 26
# ..$ interest: chr [1:2] "sports" "music"
# ..$ lang :List of 3
# .. ..$ r : num 3
# .. ..$ java: num 2
# .. ..$ cpp : num 5
# $ p3:List of 4
# ..$ name : chr "Penny"
# ..$ age : num 25
# ..$ interest: chr [1:2] "movies" "reading"
# ..$ lang :List of 3
# .. ..$ r : num 1
# .. ..$ cpp : num 4
# .. ..$ python: num 2
str(list.update(devs,interest=NULL,lang=NULL,nlang=length(lang)))
# List of 3
# $ p1:List of 3
# ..$ name : chr "Ken"
# ..$ age : num 24
# ..$ nlang: int 3
# $ p2:List of 3
# ..$ name : chr "James"
# ..$ age : num 25
# ..$ nlang: int 3
# $ p3:List of 3
# ..$ name : chr "Penny"
# ..$ age : num 24
# ..$ nlang: int 3