Cisco
Cisco

Reputation: 137

Problems with a merge inside a loop

I have a specific data.frame (data) that I would like to merge with more than 40 other data.frames (df.1 df.40) always using the same column "IID". I first create a vector with the df.n. The first 5:

names<-c(df.1,df.2,df.3,df.4,df.5)

Then I tried to use this script without success:

lst<-list()
for (i in names){
lst[[i]]<-merge(data,[i],by="IID")
}

Then my intention was to to this:

new_names<-c(d.1,d.2,d.3,d.4,d.5)

for (i in 1:length(lst)) {
  assign(new_names[i], lst[[i]])
}

However I could not get right the first loop. something is wrong with how I can the loop at the merge function.

Upvotes: 0

Views: 62

Answers (1)

Alejandro Andrade
Alejandro Andrade

Reputation: 2216

You are missing the get so it actually gets the dataframe you want to merge. Like this:

lst<-list()
for (i in names){
  lst[[i]]<-merge(data,get(i),by="IID")
}

Upvotes: 1

Related Questions