lapply on text files to make one single data frame in R

Viewed 157

I have few text files which are named as follows: abc.txt, def.txt and so on(just showing 2 text files here)

abc.txt has following lines in it:

ID  Inc
1   0.3
2   -0.2
3   0.45
4   0.12

and def.txt looks like this:

ID  Inc
2   0.13
5   0.11
6   0.43

I want to have final output data frame like this:

abc 0.3
abc -0.2
abc 0.45
abc 0.12
def 0.13
def 0.11
def 0.43

Here is what I have tried:

files <- list.files(path = baseDir, pattern = "*.txt",full.names = TRUE,
                    recursive = FALSE)
lapply(files, function(x){
  a = basename(x)
  a = gsub("\\..*","",a)
  df <- read.table(x, header = TRUE, sep ="\t")
  df$type <- a
  newDF <- data.frame(df$type,df$IncLevelDifference)
})

Since I am applying lapply, I am getting that many number of dataframes(as the number of input files). Also I want to combine it into one data frame(for downstream analysis, so I hope this newDF is available somehow outside the lapply loop) with or without the presence of headers in final output dataframe. I can add the column names separately.

1 Answers

Suppose you have a files-vector like this:

#> files
#> [1] "abc.txt" "def.txt"

You could use purrr:

library(purrr)

files %>%
  `names<-`(., gsub("\\..*", "", .)) %>% 
  map_df(~ read.table(., header = TRUE, sep ="\t"),
         .id = "ID")

which returns

   ID   Inc
1 abc  0.30
2 abc -0.20
3 abc  0.45
4 abc  0.12
5 def  0.13
6 def  0.11
7 def  0.43

The first two lines create a named vector

#>       abc       def 
#> "abc.txt" "def.txt" 

The function map_df uses this vector as input and returns a data.frame after the function inside of map_df has been executed. The elements of the named vector are used as first argument of the read.table()-function. This function returns a data.frame like shown in your question, but map_df's .id = "ID" argument replaces the ID column with the name stored in the named vector.

Related