r - 在R列表中的每个元素中添加新值?

标签 r list

这里是list1,里面只有两个元素——“name”和“age”,每个元素有两个值,现在我想在每个元素中添加新的值,

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

还有更简单的方法吗?

最佳答案

此解决方案适用于任何长度的列表:

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

关于r - 在R列表中的每个元素中添加新值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13263974/

相关文章:

r - 使用 R 时尝试替换 NA 时得到奇数值

r - 是否有一个 r 函数可以帮助我找到携带特定年份数据的行数?

python - 使用 Python 列出列表中重复值的索引

python - 在 Python 中使用列表方法加速 for 循环

python - 按倒数第二个字符对字符串列表进行排序

r - R中的igraph - 找到所有可访问的顶点

r - 为什么每次 R 升级时都必须手动创建目录 "~/R/%p-library/%v"?

R:为什么 group_by 即使在使用 quosures 时仍然需要 "do"

python - 在 Python 中使用元组或列表对字典进行排序

python - 比较和排序两个列表