Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为在for循环中创建的对象指定唯一名称_R_For Loop_Assign_Rda - Fatal编程技术网

为在for循环中创建的对象指定唯一名称

为在for循环中创建的对象指定唯一名称,r,for-loop,assign,rda,R,For Loop,Assign,Rda,我正在编写一个循环,其中每个迭代的输出都必须保存为.rda文件 假设我有一个10个位置的向量,叫做'location.id' dat <- data.frame(location.id = rep(c(00,11,22,33,44,55,66,77,88,99), each = 10), x = runif(10*10)) location.id <- c(00,11,22,33,44,55,66,77,88,99) 所有文件都加载为DT.grid。我理解为什么会发生这种情况,但

我正在编写一个循环,其中每个迭代的输出都必须保存为.rda文件

假设我有一个10个位置的向量,叫做'location.id'

dat <- data.frame(location.id = rep(c(00,11,22,33,44,55,66,77,88,99), each = 10), x = runif(10*10))

location.id <- c(00,11,22,33,44,55,66,77,88,99)

所有文件都加载为
DT.grid
。我理解为什么会发生这种情况,但我不知道如何为循环中的每个.rda文件分配不同的名称

rda格式锁定变量名,因此在保存之前需要将它们设置为不同的值,因为您将它们全部保存为
DT.grid
。类似于

for(m in unique(location.id)){
   varname <- paste0("DT.grid_", m)
   assign(varname, dat[dat$location.id == m,])
   save(varname, file = paste0("temp_",m,".rda"))
}
for(m在唯一位置(location.id)){

在我看来,varname
saveRDS
会更干净,您只需将问题中的
save
更改为
saveRDS
 load(file = "temp_00.rda")
 load(file = "temp_11.rda")
 load(file = "temp_22.rda")
 load(file = "temp_33.rda")
for(m in unique(location.id)){
   varname <- paste0("DT.grid_", m)
   assign(varname, dat[dat$location.id == m,])
   save(varname, file = paste0("temp_",m,".rda"))
}