This is an example from the help page of the assign
function:
> a <- 1:4
> assign("a[1]", 2)
> a[1] == 2 # FALSE
> get("a[1]") == 2 # TRUE
Instead of creating a whole new object a[1]
, I wish I could overwrite the 1st member of the object a
. I tried this:
> get("a")[1] <- 2
Error in get("a")[1] <- 2 :
target of assignment expands to non-language object
But it didn't work. I wish get() <-
were a valid expression, but it's not. Is there any alternative for this?
What I'm trying to do is:
I have a lot of objects (data frames): x11
x12
x13
x21
x22
x23
x31
... (almost 20 of them). All of these objects contain 30 years of daily data. They were created using:
> for(i in c("x11", "x12", "x13", ...)) {
> assign(i, read.csv())}
And now, I'm attempting to work with my data using a code like this:
> for(i in c("x11", "x12", "x13", ...)) {
> for(j in 1:n) {
> get(i)[j] <- "some.input"}}
And it gives me this message:
Error in get("x11")[] <- "some.input" :
target of assignment expands to non-language object
I hope, the problem I'm facing is understandable.
Agree with David's comments above, you can probably do this with a list of data frames. Your creation code would look like:
df_list <- lapply(c("x11", "x12", "x13", ...), function(i) {
read.csv(...)
})
and your modification code:
for(i in 1:length(df_list)) {
for(j in 1:n) {
df.list[[i]][j] <- "some.input"}}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.