[R] R help: Batch read files based on names in a list

arun smartpink111 at yahoo.com
Thu May 16 07:54:48 CEST 2013


HI,
You could use:
(# with 3 files in my folder)
lfiles<-list.files(pattern=".txt")
 lfiles
#[1] "file1.txt" "file2.txt" "file3.txt"
lst1<-lapply(lfiles,function(x) read.table(x,header=TRUE,sep="",stringsAsFactors=FALSE))
lst1
#[[1]]
#  col1 col2
#1    1  0.5
#2    2  0.2
#3    3  0.3
#4    4  0.3
#5    5  0.1
#6    6  0.2
#
#[[2]]
 # col1 col3
#1    1    A
#2    2    B
#3    3    C
#
#[[3]]
 # col1 col4
#1    1  0.1
#2    2  0.5
#3    4  0.9
library(plyr)
 join_all(lst1,by="col1")
#  col1 col2 col3 col4
#1    1  0.5    A  0.1
#2    2  0.2    B  0.5
#3    3  0.3    C   NA
#4    4  0.3 <NA>  0.9
#5    5  0.1 <NA>   NA
#6    6  0.2 <NA>   NA

join_all(lst1,by="col1",type="inner")
#  col1 col2 col3 col4
#1    1  0.5    A  0.1
#2    2  0.2    B  0.5

#or
 Reduce(function(...) merge(...,all=TRUE),lst1)
#  col1 col2 col3 col4
#1    1  0.5    A  0.1
#2    2  0.2    B  0.5
#3    3  0.3    C   NA
#4    4  0.3 <NA>  0.9
#5    5  0.1 <NA>   NA
#6    6  0.2 <NA>   NA

#Suppose you don't want to read "file3.txt"
 lfilesSub<-lfiles[!lfiles%in% "file3.txt"]
lfilesSub
#[1] "file1.txt" "file2.txt"

A.K.








----- Original Message -----
From: Jonathan Dry <dryjon at gmail.com>
To: r-help at r-project.org
Cc: 
Sent: Wednesday, May 15, 2013 1:51 PM
Subject: [R] R help: Batch read files based on names in a list

*

I am currently reading in a series of files, applying the same functions to
them one at a time, and then merging the resulting data frames e.g.:

>MyRows <- c("RowA", "RowB", "RowC")>>File1_DF <- read.delim("\\\\DirectoryToFiles\\File1_Folder\\File1.txt", stringsAsFactors=FALSE, check.names=FALSE)>File1_DF <- as.data.frame(t(File1_DF[MyRows,]))>File1_DF <- as.data.frame(t(File1_DF))>mergeDF <- merge(mergeDF,File1_DF, by.x = "Row.names", by.y="row.names")>>File2_DF <- read.delim("\\\\DirectoryToFiles\\File2_Folder\\File2.txt", stringsAsFactors=FALSE, check.names=FALSE)>File2_DF <- as.data.frame(t(File2_DF[MyRows,]))>File2_DF <- as.data.frame(t(File2_DF))>mergeDF <- merge(mergeDF,File2_DF, by.x = "Row.names", by.y="row.names")

...etc

I want to know if I can use a list of the filenames c("File1", "File2",
"File2") etc. and apply a function to do this in a more automated fasion?
This would involve using the list value in the directory path to read in
the file i.e.

>*MyFilesValue*_DF <- read.delim("\\\\DirectoryToFolders\\*MyFilesValue*_Folder\\*MyFilesValue*.txt", stringsAsFactors=FALSE, check.names=FALSE)

Any help appreciated
*

    [[alternative HTML version deleted]]

______________________________________________
R-help at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.




More information about the R-help mailing list