My question is two fold. First, given these three data frames:
df1 <- data.frame(k1 = runif(6, min=0, max=100), 
             k2 = runif(6, min=0, max=100), 
             k3 = runif(6, min=0, max=100), 
             k4 = runif(6, min=0, max=100))
df2 <- data.frame(k1 = runif(6, min=0, max=100), 
              k2 = runif(6, min=0, max=100), 
              k3 = runif(6, min=0, max=100), 
              k4 = runif(6, min=0, max=100))
df3 <- data.frame(k1 = runif(6, min=0, max=100), 
              k2 = runif(6, min=0, max=100), 
              k3 = runif(6, min=0, max=100), 
              k4 = runif(6, min=0, max=100))
I would like to reformat and rename part of each data frame using this function:
samplelist<-c("k2", "k4")
draft_fxn<-function(x, obj_name){
  x.selected<-x[,c(samplelist)] #select columns of choice
  colnames(x.selected)[1:2]<-paste(obj_name, colnames(x.selected), sep="_") #rename columns so they include original data frame name
  return(x.selected)
}
#Example run and output:
df2_final<-draft_fxn(df2, "df2")
#output from:
head(df2_final[1:2],)
>     df2_k2   df2_k4
>1  5.240274 53.03423
>2  5.042926 34.78974
First question: How can I change my function so I don't have to type in ' df2, "df2" '. In my draft_fxn code, I want to replace "obj_name" with whatever the name of the input data frame is. In my example it is "df2".
Second question: How can I loop through all of my data frames? Perhaps, similar to this for loop? objs<-c(df1, df2, df3)
for (file in objs){
  out<-draft_fxn(file); return(out)
} #this doesn't work though. 
 
     
    