0

I am new using dplyr package and I have been trying to read multiple files in R and then create a data frame by binding all the rows, but including the name of each file as a new column. This new column is the corresponding date which is not included in the data.

My list of files (for example):
01012019.aps
02012019.aps

I would like to have my final dataframe like this:

x y file date 1 4 01012019 01-01-2019 2 5 01012019 01-01-2019 3 6 02012019 02-01-2019 4 7 02012019 02-01-2019 

I've been trying this:

path_aps<- "C:/Users/.../.../APS" files_aps <- list.files(path_aps, pattern = "*.aps") data_aps <- files_aps %>% map(~ read.table(file.path(path_aps, .), sep = "\t")) %>% map(~ mutate(filename = files_aps, .))%>% reduce(gtools::smartbind) 

But I am getting this error:

Error: Column filename must be length 288 (the number of rows) or one, not 61

I understand that the list of files in files_aps has 61 elements as this is the number of files that I have in my directory and 288 is the number of rows of each .aps file; however, I haven't been able to make it work to the extend of each .aps file. I've been reading multiple answers to similar questions but still I am not getting the expected result.

0

1 Answer 1

1

I've solved it with the help of this other answer and I've got this:

data_aps <- list.files(path_aps, pattern = "*.aps", full.names = TRUE) %>% map_df(function(x) read.table(x, sep = "\t") %>% mutate(filename=gsub(".aps","", basename(x))))

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.