2017-05-24 64 views
-1

我有1000人的名字在数据帧值到代码中的“名称”数据帧插入R中

df=c("John","Smith", .... "Machine") 

,我为每个人1000个数据帧。 (例如,a1〜a1000) 而且,我有以下代码。

a1$name="XXXX" 
a2$name="XXXX" ... 
a1000$name="XXXX" 

我想用上述代码中的“XXXX”替换名称数据框中的值。输出代码看起来像这样。

a1$name="John" 
a2$name="Smith" ... 
a1000$name="Machine" 
+0

您是否有可能将data.frames(a1 ... a1000)存储到列表中?另外,请不要指出'df',因为您已经定义了它,它是一个不是data.frame的字符向量...... – digEmAll

+0

不,我不能将1000个数据帧存储为列表。感谢您的答复。 – SSP

+0

你可以发布str(a1)结果吗? –

回答

0

首先,你需要将它们组合成列表。(我不知道它是否与1000数据帧或不工作)。

df=c("John","Smith", .... "Machine") 

list_object_names = sprintf("a%s", 1:1000) 
list_df = lapply(list_object_names, get) 

for (i in 1:length(list_df)){ 

    list_df[[i]][,'Names']=df[i] 
} 

您也可以尝试申请功能,而不是循环的东西如:

lapply(list_df, function(df) { 
    #what you want to do 
}) 
+0

感谢您的回复。实际上,我无法创建“列表”。我的问题是如何创建“代码”而不是组织数据框和列表。对困惑感到抱歉。 – SSP

0

这是我在这个镜头,不知道是否有任何更多的a1,a2 ... a1000列表。

# generate your data 
df = c("John", "Smith", "Machine") 

# build your example 
for(i in 1:3){ 
    assign(paste0("a",i), list(name = "XXXX")) 
} 

# solve your problem, even if there is more to a1 than you are showing us. 
for(i in 1:3){ 
    anew <- get(paste0("a",i)) # pulls the object form the environment 
    anew[['name']] <- df[i] # rewrites only that list 
    assign(paste0("a",i), anew) # rewrites the object with new name 
} 
+0

感谢您的回复。实际上,我无法创建“列表”。我的问题是如何创建“代码”而不是组织数据框和列表。对困惑感到抱歉。 – SSP

+0

我很困惑。我正在创造你想要的输出。 –

+0

?你是什​​么意思创建代码?我将名称分配到上面的代码点 –