Cynthia Sadler
2009-Sep-21 16:19 UTC
[R] Error in make.names when trying to read.table in if statement
Hi, I'm trying to read data from a collection of CSV files for processing and graphing. All of my files begin with "modrate" and end with ".csv". I think I have the regex working but I am stumped at trying to get read.table to work within an if statement. This works: > filepattern="modrate*" > files <- list.files(pattern=filepattern) > data <- read.table(files[1], header=TRUE, sep=",") But I cannot get this to work: > for (i in seq(along=files)) { + data <- read.table(files[i], header=TRUE, sep=",") + } Error in make.names(col.names, unique = TRUE) : invalid multibyte string at '<ff><d8><ff><e0>' I'm sure I'm making a newbie mistake here. I'm using R version 2.9.2 (2009-08-24) on Mac OS X, and didn't find anything about this in the help archives (though, as I'm new to this, I may not have searched in the best way). Any advice? Thanks. Kind regards, Cynthia
Juliet Hannah
2009-Sep-25 21:49 UTC
[R] Error in make.names when trying to read.table in if statement
Does this work for you? data_list <- list() filepattern="modrate*" all_files <- list.files(pattern=filepattern) data_list <- lapply(all_files, read.table,header=TRUE,sep=",")
David Winsemius
2009-Sep-25 22:23 UTC
[R] Error in make.names when trying to read.table in if statement
On Sep 21, 2009, at 12:19 PM, Cynthia Sadler wrote:> Hi, > > I'm trying to read data from a collection of CSV files for > processing and graphing. All of my files begin with "modrate" and > end with ".csv". I think I have the regex working but I am stumped > at trying to get read.table to work within an if statement. > > This works: > > > filepattern="modrate*" > > files <- list.files(pattern=filepattern) > > data <- read.table(files[1], header=TRUE, sep=",") > > But I cannot get this to work: > > > for (i in seq(along=files)) { > + data <- read.table(files[i], header=TRUE, sep=",") > + } > Error in make.names(col.names, unique = TRUE) : > invalid multibyte string at '<ff><d8><ff><e0>'The error message suggests that you might have strange characters in your header lines which make.names() is choking on. What is the result of substituting readLines with parameter of n=1 for the read.table() call on those files? Or perhaps you should be using "[[" rather than "[" to access the files list?> > I'm sure I'm making a newbie mistake here.Despite several years, I still consider myself a newbie.> I'm using R version 2.9.2 (2009-08-24) on Mac OS X, and didn't find > anything about this in the help archives (though, as I'm new to > this, I may not have searched in the best way). Any advice? Thanks.David Winsemius, MD Heritage Laboratories West Hartford, CT