0

I have a number of files that I have to merge and the name of the dataframe they will merge into is given in a variable. It looks like I got somehow stuck in R naming conventions. A minimum (silly) examples of what I intend to do is below.

Update code updated to be stand alone and added the solution proposed by @DaveArmstrong

tump<-data.frame(n="imp", a=1, b=2)
tamp<-data.frame(n="imp", a=3, b=4)
timp<-data.frame(n="base", a=5, b=6)
# This works
eval(as.name(paste("t" ,tump$n, sep="")))
# Returns timp
#  n a b
# 1 base 5 6
#*****************************
#This works
rbind(eval(as.name(paste("t" ,tump$n, sep=""))), tamp)
# Merges timp and tamp
#       n a b
#  1 base 5 6
#  2  imp 3 4
#
# ERROR
eval(as.name(paste("t" ,tump$n, sep="")))<-
 rbind(eval(as.name(paste("t" ,tump$n, sep=""))), tamp)
#
#Error in eval(as.name(paste("t", tump$n, sep = ""))) <- rbind(eval(as.name(paste("t",  : 
#target of assignment expands to non-language object
#
# This is the solution proposed by Dave and it works
result<-assign(paste("t" ,tump$n, sep=""), rbind(get(paste("t" ,tump$n, sep="")), tamp))
result


Frank
  • 26
  • 2
  • 1
    what is `tump`, was that meant to be `timp` and can you provide tbase as well? – Mike Feb 15 '22 at 14:30
  • 1
    I wonder if this might work better: `assign(paste("t" ,tump$n, sep=""), rbind(get(paste("t" ,tump$n, sep="")), tamp))` – DaveArmstrong Feb 15 '22 at 14:33
  • 1
    You can't have `eval()` on the left hand side of an assignment operator. You can use `assign()` as DaveArmstrong suggests. But this is all pretty terrible--hard to read and bug-prone. Consider [using lists instead](https://stackoverflow.com/a/24376207/903061). – Gregor Thomas Feb 15 '22 at 14:45
  • Thank you . Dave's comment did the trick. – Frank Feb 15 '22 at 18:50
  • I think that @GregThomas's solution is a good thing to keep in mind. I might (or might not) go for it in the code. – Frank Feb 15 '22 at 18:58

0 Answers0