[R] Reading a list of filenames from a csv file
arun
smartpink111 at yahoo.com
Thu Jul 11 16:56:17 CEST 2013
Hi,
Try this:
files1<-read.csv("files.csv",header=TRUE,stringsAsFactors=FALSE)
str(files1)
#'data.frame': 2 obs. of 2 variables:
# $ Col1: chr "ANA110915004A_3PERIOD_TmAvg-rdata.csv" "ANA110915006A_3PERIOD_TmAvg-rdata.csv"
# $ Col2: chr "Pre-DA" "DA-10^-6"
files1
# Col1 Col2
#1 ANA110915004A_3PERIOD_TmAvg-rdata.csv Pre-DA
#2 ANA110915006A_3PERIOD_TmAvg-rdata.csv DA-10^-6
#Using some fake data
lapply(seq_len(nrow(files1)),function(i) {x1<-read.csv(file=files1[i,1],header=TRUE,sep="",check.names=FALSE);x1[files1[i,2]]})
[[1]]
# Pre-DA
#1 2
#2 3
#3 6
#4 4
#[[2]]
# DA-10^-6
#1 9
#2 14
#3 13
#4 21
Hope this helps.
A.K.
----- Original Message -----
From: Jannetta Steyn <jannetta at henning.org>
To: r-help <r-help at r-project.org>
Cc:
Sent: Thursday, July 11, 2013 9:01 AM
Subject: [R] Reading a list of filenames from a csv file
What would be the best way to read a list of filenames and headings from a
csv file?
The CSV file is structured as two columns, with column one being the
filename and column two being a heading e.g.:
ANA110915004A_3PERIOD_TmAvg-rdata.csv,Pre-DA
ANA110915006A_3PERIOD_TmAvg-rdata.csv,DA-10^-6
ANA110915012A_3PERIOD_TmAvg-rdata.csv,DA-10^-4
ANA110915016A_3PERIOD_TmAvg-rdata.csv,Washout
I want to be able to open the file using read.csv and use the heading as
the header of a graph.
Reading the filenames from the directory with list.files() works but then I
don't have the headings that go with the file e.g.:
filenames<-list.files(pattern="*.csv")
for (i in seq_along(filenames)) {
con<-read.csv(filenames[i], headers=TRUE, sep=',')
}
I tried the code below (which I posted in a different thread) but the
solutions that people offered me didn't get it to work. The code results in
'Error in read.table(file = file, header = header, sep = sep, quote =
quote, :
'file' must be a character string or connection
# Read filenames from csv file
files <- read.csv(file="files.csv",head=FALSE,sep=",")
# for each filename read the file
for (i in 1:length(files)) {
# f becomes the next row inthe file
f<-files[i,]
# the header to be used for the graph is in column 2 of f
head=f[2]
par(mfrow=c(4,2))
# the filename to be used is in column 1 of f
con<-read.csv(file=f[1], header=TRUE, sep=',')
tmp<-con$value2
data<-normalize_js(tmp,-1,1)
time<-con$time
# run the waveform analyser
waveformanalyser(data,time,head)
}
Regards
Jannetta
--
===================================
Web site: http://www.jannetta.com
Email: jannetta at henning.org
===================================
[[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