I have an array(pstype) whose elements are column names for another data frame, i would like to call a column name[Array Element] one by one from that array by attaching ".x" and ".y" with it and put those columns in a new data frame(new_df):
eg:
pstype<-c("eid","esal","exp")
df2:
eid esal.x exp.x esal.y exp.y
2 4 1 4 1
3 4 2 4 2
4 4 3 4 3
5 3 3 3 3
df1:
eid
2
3
4
5
This is what i am trying to do:
for(i in 1:length(pstype)){
xpstype<-paste(pstype[i],".x",sep="")
ypstype<-paste(pstype[i],".y",sep="")
new_df<-merge(df1,df2[,c("eid",xpstype,ypstype)],by="eid")
}
The error that i'm getting is: by passing an object to c("eid",xpstype,ypstype)
Error: undefined columns selected