[R] Importing multiple text files with lapply.
Ivan Calandra
ivan.calandra at uni-hamburg.de
Tue Jan 18 09:50:57 CET 2011
Hi,
The solution with lapply() should work; it seems that the problem was
with the file name.
mylist <- lapply(a, read.table, header = TRUE, sep = '\n')
What is "a"? Is it an object containing a path+file name? Maybe not.
Second, I'm not sure sep should be "\n". I think "," would be better.
Just a guess.
By the way, wouldn't it be easier if you put all the data in a single
file? You could add a column with "state". Just my humble opinion.
Ivan
Le 1/18/2011 02:52, Simon Kiss a écrit :
> readLines worked great Jim, thanks!
> Simon Kiss
> On 2011-01-17, at 7:44 PM, jim holtman wrote:
>
>> It sounds like you want to use 'readLines' and not 'read.table'
>>
>>> x<- readLines(textConnection("January 11, 2009
>> + January 11, 2009
>> + October 19, 2008
>> + October 13, 2008
>> + August 16, 2008
>> + June 19, 2008
>> + April 19, 2008
>> + April 16, 2008
>> + February 9, 2008
>> + September 2, 2007"))
>>> closeAllConnections()
>>> x
>> [1] "January 11, 2009" "January 11, 2009" "October 19, 2008"
>> "October 13, 2008" "August 16, 2008"
>> [6] "June 19, 2008" "April 19, 2008" "April 16, 2008"
>> "February 9, 2008" "September 2, 2007"
>> What exactly are you going to do with the data after you read it in?
>>
>> On Mon, Jan 17, 2011 at 6:22 PM, Simon Kiss<simonjkiss at yahoo.ca> wrote:
>>> Dear jim,
>>> Yes, it's true, the data are separated onto new lines as follows:
>>> January 11, 2009
>>> January 11, 2009
>>> October 19, 2008
>>> October 13, 2008
>>> August 16, 2008
>>> June 19, 2008
>>> April 19, 2008
>>> April 16, 2008
>>> February 9, 2008
>>> September 2, 2007
>>>
>>> I tried your attempt and it didn't work either; it returned the error message:
>>> Error in FUN(X[[1L]], ...) :
>>> 'file' must be a character string or connection
>>>
>>> On 2011-01-17, at 2:02 PM, jim holtman wrote:
>>>
>>>> try:
>>>>
>>>> mylist<- lapply(a, read.table, header = TRUE, sep = '\n')
>>>>
>>>> also is the separator really '\n' meaning a new-line? What exactly
>>>> does the data look like?
>>>>
>>>> On Mon, Jan 17, 2011 at 11:47 AM, Simon Kiss<simonjkiss at yahoo.ca> wrote:
>>>>> Hello,
>>>>> I'm trying to read in 50 text filess with dates as content to create a list of tables.
>>>>>
>>>>> a is the list of filenames that need to be read in.
>>>>>
>>>>> The following command returns the following error
>>>>> mylist<-lapply(a, read.table(header=TRUE, sep="\n"))
>>>>>
>>>>> Error in read.table(header = TRUE, sep = "\n") :
>>>>> element 1 is empty;
>>>>> the part of the args list of 'is.character' being evaluated was:
>>>>> (file)
>>>>>
>>>>> Does anyone have any suggestions?
>>>>> Yours, Simon Kiss
>>>>> *********************************
>>>>> Simon J. Kiss, PhD
>>>>> Assistant Professor, Wilfrid Laurier University
>>>>> 73 George Street
>>>>> Brantford, Ontario, Canada
>>>>> N3T 2C9
>>>>> Cell: +1 519 761 7606
>>>>>
>>>>> ______________________________________________
>>>>> 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.
>>>>>
>>>>
>>>>
>>>> --
>>>> Jim Holtman
>>>> Data Munger Guru
>>>>
>>>> What is the problem that you are trying to solve?
>>> *********************************
>>> Simon J. Kiss, PhD
>>> Assistant Professor, Wilfrid Laurier University
>>> 73 George Street
>>> Brantford, Ontario, Canada
>>> N3T 2C9
>>> Cell: +1 519 761 7606
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>
>>
>> --
>> Jim Holtman
>> Data Munger Guru
>>
>> What is the problem that you are trying to solve?
> *********************************
> Simon J. Kiss, PhD
> Assistant Professor, Wilfrid Laurier University
> 73 George Street
> Brantford, Ontario, Canada
> N3T 2C9
> Cell: +1 519 761 7606
>
> ______________________________________________
> 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.
>
--
Ivan CALANDRA
PhD Student
University of Hamburg
Biozentrum Grindel und Zoologisches Museum
Abt. Säugetiere
Martin-Luther-King-Platz 3
D-20146 Hamburg, GERMANY
+49(0)40 42838 6231
ivan.calandra at uni-hamburg.de
**********
http://www.for771.uni-bonn.de
http://webapp5.rrz.uni-hamburg.de/mammals/eng/1525_8_1.php
More information about the R-help
mailing list