Storing a content in to a desired object

I have a vector like this :

vec1 <- c("x1","x2","x3","x4")

I have another vector like this :

vec2 <- c("X1.RData","X2.RData","X3.RData","X4.RData")

What I am trying to do here is to load the RData files and then save them back with the same file names but the objects should be that of the vec1.

for (i in 1:length(vec1)){
vec1[i] <- get(load(vec2[i]))
save(vec1[i],file=vec2[i])
}

but its giving me error saying "incompatible types" I also tried changing the class of the character objects like this :

class(vec1[i]) <- myclass

where myclass is class of the S4 type object.

Is there a way to do this in R ???

Answers


You will need to know the names of the objects in each of the .Rdata files. Object names are saved with the object contents and when accessed using load do not get assigned the names of the files in which they are stored. Furthermore, you would need to use assign in order to give those objects a new name. Assuming you had nothing else in the workspace at the time:

Untested:

for (i in 1:length(vec1)){ load(vec2[i])
   assign( vec1[i] , get( ls() )
   save(vec1[i],file=vec2[i])
   rm(list=ls(patt="^x.$") )
}

If you have other things in the workspace, then you really do need to know the names of the objects in those .Rdata files. (Although if you load()-ed these into an specific environment, you might be able to insulate the process from the rest of your workspace/)


Need Your Help

Using Thread in a window 8 application

c# windows-runtime

Is there a way to use the Thread class inside a No-Longer-Named-Metro application?

Using BeginExecuteNonQuery with async tasks

c# .net asynchronous task

I'm working on adding some methods to a base repository to handle other code that will process queries that use async Tasks. I am basically just wraping a Task around the existing BeginExecuteNonQu...