assign

How to name variables on the fly?

自闭症网瘾萝莉.ら 提交于 2019-11-25 23:20:54
问题 Is it possible to create new variable names on the fly? I\'d like to read data frames from a list into new variables with numbers at the end. Something like orca1, orca2, orca3... If I try something like paste(\"orca\",i,sep=\"\")=list_name[[i]] I get this error target of assignment expands to non-language object Is there another way around this? 回答1: Use assign : assign(paste("orca", i, sep = ""), list_name[[i]]) 回答2: It seems to me that you might be better off with a list rather than using

Why is using assign bad?

六眼飞鱼酱① 提交于 2019-11-25 21:58:47
问题 This post (Lazy evaluation in R – is assign affected?) covers some common ground but I am not sure it answers my question. I stopped using assign when I discovered the apply family quite a while back, albeit, purely for reasons of elegance in situations such as this: names.foo <- letters values.foo <- LETTERS for (i in 1:length(names.foo)) assign(names.foo[i], paste(\"This is: \", values.foo[i])) which can be replaced by: foo <- lapply(X=values.foo, FUN=function (k) paste(\"This is :\", k))