Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
考虑以下代码
names = c("1","2","3") values = c(1.0,1.5,0.5) list(names = values)
这将产生以下列表
$names [1] 1.0 1.5 0.5
但我想要的清单是:
$`1` [1] 1 $`2` [1] 1.5 $`3` [1] 0.5
换句话说,list("1"=1.0,"2"=1.5,"3"=0.5)
list("1"=1.0,"2"=1.5,"3"=0.5)
那么,如何从变量开始创建这样的列表names呢values?
names
values
您可以使用setNames和as.list:
setNames
as.list
setNames(as.list(values), names) # $`1` # [1] 1 # # $`2` # [1] 1.5 # # $`3` # [1] 0.5
Map(function(x,y) y,names,values) $`1` [1] 1 $`2` [1] 1.5 $`3` [1] 0.5