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.