0

This might be a dumb question, but I can't seem to find a solution. I have the following lines of code

name1 <- paste("pred.return.Tr", trainInt, ".Te", testInt, sep = "")
assign(name1, as.data.frame(matrix(, dim(dfVar[1])-NullR, 3, TRUE)))

So now that I have my df created and it's name stored in name1 I am trying to manipulate value and metrics of the data frame. Their are two things I need to do but can't see how. First is to change the column names and second is to have value inputed. I was thinking the following but I get the error could not find function "get<-";

colnames(get(name1)) <- c("Return", "GrossReturn", "CummReturn")
get(name1)[1,1] <- some value

Appreciate the help, pretty sure there is a simple solution. I just can't think of one for some damn reason. I'm new to coding so don't be harsh

Chris Kitching
  • 2,559
  • 23
  • 37
  • 3
    My usual advice in these cases is to keep your data frames in named lists, not isolated in your workspace. Then things are quite simple, and there's generally no need for `assign` or `get`. But people sometimes really don't like that advice. – joran Mar 10 '16 at 16:30
  • I agree with joran. Especially if you are new to coding, this is a bad habit to get in to. This is not usually how you approach this problem in R (it's more common with, say, SAS). It's better if you describe the problem you are really wring to solve rather than trying to make this particular solution work. – MrFlick Mar 10 '16 at 16:34

1 Answers1

0

I think if you really want to do this you have to use incantations involving eval(), substitute(), as.symbol ... however, as the comments suggest, this is extremely awkward because it's non-idiomatic R.

name1 <- "junk"
assign(name1, data.frame(1:3,1:3,1:3))

eval(substitute(colnames(n) <- c("Return", "GrossReturn", "CummReturn"),
           list(n=as.symbol(name1))))
eval(substitute(n[1,1] <- 14,
           list(n=as.symbol(name1))))

There might be a slightly more direct way to do this (I'm not very good at eval() etc.), but it probably can't be done much more easily.

If you instead put your data frames into a list (if you're trying to use this sort of indirect addressing, presumably you have a lot of them ...), then all of this becomes easy. For example:

myStuff <- list(junk=junk)
names(myStuff)  ## "junk"

To set column names or values:

colnames(myStuff[[name1]]) <- c("a","b","c")
myStuff[[name1]][1,1] <- 57

If you have more than one data frame, you can either put them in the list at the beginning:

myStuff <- list(junk=junk,junk2=junk)

or add them on the fly

myStuff[["junk3"]] <- junk

Actually, it occurs to me that there is a very hacky way to do this, which is to treat your global environment as a sort of list (actually it's an environment, but it can be indexed in the same way as a list ...

colnames(.GlobalEnv[[name1]]) <- c("d","e","f")
.GlobalEnv[[name1]][1,1] <- 64
Ben Bolker
  • 211,554
  • 25
  • 370
  • 453
  • Thank you so much, this actually helps a ton. Just a quick question since I haven't used this. How exactly do you create list with multiple data frames, or how do you add data frames to the list? – agustino.davalos Mar 12 '16 at 02:23