1

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!

1
  • The last assignment in a function will be returned as values if there is no further return argument. Therefore, add one line return(df) or just df to your function, to return the data frame. Commented Dec 14, 2020 at 18:35

2 Answers 2

2

It can be:

#Code tucson_function<- function (x) { df <- read.csv(x) tag <- sub('\\.csv$', '', x) df$tag <- tag } 

Or:

#Code tucson_function<- function (x) { df <- read.csv(x) tag <- sub('\\.csv$', '', x) names(df) <- paste0(tag,'.',names(df)) return(df) } 
Sign up to request clarification or add additional context in comments.

Comments

1

We can use transform with tools::file_path_sans_ext to create a column

my_function<- function(x) { df <- read.csv(x) transform(df, tag = tools::file_path_sans_ext(x)) } 

and then call the function with lapply

lapply(my_list, my_function) 

In the OP's function the issue seems to be that the return is the last assignment i.e. the column names assignment. We need to return the data i.e. 'df'

my_function<- function (x) { df <- read.csv(x) tag <- sub('\\.csv$', '', x) colnames(df) <- paste0(tag, colnames(df)) df } 

3 Comments

Hi! Thank you. I am getting the error that Error: 'file_paths_sans_ext' is not an exported object from 'namespace:tools
@salix_august sorry, a typo. it would be path instead of paths in file_path_sans_ext
@salix_august as an example tools::file_path_sans_ext("file1.csv")# [1] "file1"

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.