I am importing a list of several dataframes using a custom function. I want to take the name of the imported file (e.g. file1 from file1.csv) and add it onto all of the column names in that dataframe. In this example, all column names will look like this:
# Column names as they are q1 q2 q3 # Column names with added name of the file they come from q1_file1 q2_file1 q3_file1 This is what I've tried, but it doesn't work (the list ends up having 0 dataframes):
my_function<- function (x) { df <- read.csv(x) tag <- sub('\\.csv$', '', x) colnames(df) <- paste0(tag, colnames(df)) } lapply(my_list, my_function) Thanks!
return(df)or justdfto your function, to return the data frame.