为现有向量中的每个元素创建新向量?

时间:2018-07-17 07:27:51

标签: r

例如,我已有一个矢量(类型:字符)

A = c("Monday","Tuesday","Wednesday","Thursday","Friday")

现在,我想为现有向量中的所有元素创建一个新的空向量。不像

那样手动
(Monday=c(), Tueday=c(),...)

我尝试了for循环

for (i in A) {
 print(i) =c()
 }

但这没用,希望您有建议。

1 个答案:

答案 0 :(得分:1)

for (i in A) {
  assign(i, c())
}

# Now list all objects in workspace
ls()
[1] "A"         "Friday"    "i"         "Monday"   
[5] "Thursday"  "Tuesday"   "Wednesday"

但这是我要分配空向量而不是c()的方式:

vector(mode = "character", length = n) 
# where n = 0 in your case