here is list1
, only tow elements--"name
" and "age
" in it,there are two value in every element ,now i want to add new value in every element,
list1<-list(name=c("bob","john"),age=c(15,17))
list1
$name
[1] "bob" "john"
$age
[1] 15 17
list1[[1]][3]<-"herry"
list1[[2]][3]<-17
list1
$name
[1] "bob" "john" "herry"
$age
[1] 15 17 17
is there more simple way to do ?
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.
The list() function in R is used to create a list of elements of different types. A list can contain numeric, string, or vector elements.
This solution works for lists of any length:
values <- list("herry", 17) # a list of the new values
list1 <- mapply(append, list1, values, SIMPLIFY = FALSE)
# $name
# [1] "bob" "john" "herry"
# $age
# [1] 15 17 17
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