rbind common header to file list

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

rbind common header to file list

Miluji Sb
Dear all,

I have more than 200 text files in a folder without header - example below.
I would like to read, add a common date header to all the files, and write
(replace) the files.

## Read files
filelist = list.files(pattern = ".*.txt")
datalist = lapply(filelist, function(x)read.table(x, header=F))

## What I want to add
date <- 20000101
datalist _new <- lapply(datalist, function(x) rbind(x, date))

How do I add this date as a common header and replace the files? Any help
will be highly appreciated. Thank you.

Best,

Milu

## Sample data
xy <- dput(head(x,6))
structure(list(V1 = c("-5.28082885742185,-0.509039307",
"-6.09873046874998,-0.349584961",
"-2.07150878906248,6.264276123", "-1.11102905273435,6.365716553",
"2.37749633789065,14.57106934", "4.9619079589844,18.91350708"
)), row.names = c(NA, 6L), class = "data.frame")

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.
Reply | Threaded
Open this post in threaded view
|

Re: rbind common header to file list

Jeff Newmiller
a) I recommend _not_ overwriting the input files. Very difficult to debug/recover if anything goes wrong.

b) I recommend making a function that takes the file name, source directory, and destination directory, and reads the file, makes the change, and writes it to the output directory.

c) Your life may be easier if you use readLines() and writeLines rather than a more sophisticated parsing function like read.table(). Adding a new string to a vector of strings is straightforward.

On January 19, 2021 3:45:53 PM PST, Miluji Sb <[hidden email]> wrote:

>Dear all,
>
>I have more than 200 text files in a folder without header - example
>below.
>I would like to read, add a common date header to all the files, and
>write
>(replace) the files.
>
>## Read files
>filelist = list.files(pattern = ".*.txt")
>datalist = lapply(filelist, function(x)read.table(x, header=F))
>
>## What I want to add
>date <- 20000101
>datalist _new <- lapply(datalist, function(x) rbind(x, date))
>
>How do I add this date as a common header and replace the files? Any
>help
>will be highly appreciated. Thank you.
>
>Best,
>
>Milu
>
>## Sample data
>xy <- dput(head(x,6))
>structure(list(V1 = c("-5.28082885742185,-0.509039307",
>"-6.09873046874998,-0.349584961",
>"-2.07150878906248,6.264276123", "-1.11102905273435,6.365716553",
>"2.37749633789065,14.57106934", "4.9619079589844,18.91350708"
>)), row.names = c(NA, 6L), class = "data.frame")
>
> [[alternative HTML version deleted]]
>
>______________________________________________
>[hidden email] mailing list -- To UNSUBSCRIBE and more, see
>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.

--
Sent from my phone. Please excuse my brevity.

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.
Reply | Threaded
Open this post in threaded view
|

Re: rbind common header to file list

Miluji Sb
In reply to this post by Miluji Sb
Thank you for your reply and the solution. Yes, I would like the date to be
the column header for all the files in the list.

This is what tried following your suggestion;

filelist = list.files(pattern = ".*.txt")
date <- 20000101

for (file %in% filelist){
  datalist <- read.table(file)
  write.table(datalist, file= file, col.names= date)
  }

However, I get the following error

Error: unexpected SPECIAL in "for (file %in%"


Is it something silly I am missing? Thank you again!

Best,

Milu

On Wed, Jan 20, 2021 at 1:29 AM <[hidden email]> wrote:

> I'd use a for loop. But I may be misunderstanding the q!
>
>
> filelist = list.files(pattern = ".*.txt")
> date <- 20000101
>
> for (file %in% filelist) {
>
> datalist <- read.table(file)
>
> write.table( datalist, file= file, col.names= date)
>
> }
>
> Does what I think you want.
> I would actually write to a new filename (sub folder?) To avoid disaster!
>
>
>
>
>
> On 19 Jan 2021 23:45, Miluji Sb <[hidden email]> wrote:
>
> Dear all,
>
> I have more than 200 text files in a folder without header - example
> below.
> I would like to read, add a common date header to all the files, and write
> (replace) the files.
>
> ## Read files
> filelist = list.files(pattern = ".*.txt")
> datalist = lapply(filelist, function(x)read.table(x, header=F))
>
> ## What I want to add
> date <- 20000101
> datalist _new <- lapply(datalist, function(x) rbind(x, date))
>
> How do I add this date as a common header and replace the files? Any help
> will be highly appreciated. Thank you.
>
> Best,
>
> Milu
>
> ## Sample data
> xy <- dput(head(x,6))
> structure(list(V1 = c("-5.28082885742185,-0.509039307",
> "-6.09873046874998,-0.349584961",
> "-2.07150878906248,6.264276123", "-1.11102905273435,6.365716553",
> "2.37749633789065,14.57106934", "4.9619079589844,18.91350708"
> )), row.names = c(NA, 6L), class = "data.frame")
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> 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.
>
>
>

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.
Reply | Threaded
Open this post in threaded view
|

Re: rbind common header to file list

Eric Berger
for ( file in filelist )



On Wed, Jan 20, 2021 at 2:21 PM Miluji Sb <[hidden email]> wrote:

> Thank you for your reply and the solution. Yes, I would like the date to be
> the column header for all the files in the list.
>
> This is what tried following your suggestion;
>
> filelist = list.files(pattern = ".*.txt")
> date <- 20000101
>
> for (file %in% filelist){
>   datalist <- read.table(file)
>   write.table(datalist, file= file, col.names= date)
>   }
>
> However, I get the following error
>
> Error: unexpected SPECIAL in "for (file %in%"
>
>
> Is it something silly I am missing? Thank you again!
>
> Best,
>
> Milu
>
> On Wed, Jan 20, 2021 at 1:29 AM <[hidden email]> wrote:
>
> > I'd use a for loop. But I may be misunderstanding the q!
> >
> >
> > filelist = list.files(pattern = ".*.txt")
> > date <- 20000101
> >
> > for (file %in% filelist) {
> >
> > datalist <- read.table(file)
> >
> > write.table( datalist, file= file, col.names= date)
> >
> > }
> >
> > Does what I think you want.
> > I would actually write to a new filename (sub folder?) To avoid disaster!
> >
> >
> >
> >
> >
> > On 19 Jan 2021 23:45, Miluji Sb <[hidden email]> wrote:
> >
> > Dear all,
> >
> > I have more than 200 text files in a folder without header - example
> > below.
> > I would like to read, add a common date header to all the files, and
> write
> > (replace) the files.
> >
> > ## Read files
> > filelist = list.files(pattern = ".*.txt")
> > datalist = lapply(filelist, function(x)read.table(x, header=F))
> >
> > ## What I want to add
> > date <- 20000101
> > datalist _new <- lapply(datalist, function(x) rbind(x, date))
> >
> > How do I add this date as a common header and replace the files? Any help
> > will be highly appreciated. Thank you.
> >
> > Best,
> >
> > Milu
> >
> > ## Sample data
> > xy <- dput(head(x,6))
> > structure(list(V1 = c("-5.28082885742185,-0.509039307",
> > "-6.09873046874998,-0.349584961",
> > "-2.07150878906248,6.264276123", "-1.11102905273435,6.365716553",
> > "2.37749633789065,14.57106934", "4.9619079589844,18.91350708"
> > )), row.names = c(NA, 6L), class = "data.frame")
> >
> > [[alternative HTML version deleted]]
> >
> > ______________________________________________
> > [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> > 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.
> >
> >
> >
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> 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.
>

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.
Reply | Threaded
Open this post in threaded view
|

Re: rbind common header to file list

Miluji Sb
Thank you, that was it.

Best,

Milu

On Wed, Jan 20, 2021 at 1:33 PM Eric Berger <[hidden email]> wrote:

> for ( file in filelist )
>
>
>
> On Wed, Jan 20, 2021 at 2:21 PM Miluji Sb <[hidden email]> wrote:
>
>> Thank you for your reply and the solution. Yes, I would like the date to
>> be
>> the column header for all the files in the list.
>>
>> This is what tried following your suggestion;
>>
>> filelist = list.files(pattern = ".*.txt")
>> date <- 20000101
>>
>> for (file %in% filelist){
>>   datalist <- read.table(file)
>>   write.table(datalist, file= file, col.names= date)
>>   }
>>
>> However, I get the following error
>>
>> Error: unexpected SPECIAL in "for (file %in%"
>>
>>
>> Is it something silly I am missing? Thank you again!
>>
>> Best,
>>
>> Milu
>>
>> On Wed, Jan 20, 2021 at 1:29 AM <[hidden email]> wrote:
>>
>> > I'd use a for loop. But I may be misunderstanding the q!
>> >
>> >
>> > filelist = list.files(pattern = ".*.txt")
>> > date <- 20000101
>> >
>> > for (file %in% filelist) {
>> >
>> > datalist <- read.table(file)
>> >
>> > write.table( datalist, file= file, col.names= date)
>> >
>> > }
>> >
>> > Does what I think you want.
>> > I would actually write to a new filename (sub folder?) To avoid
>> disaster!
>> >
>> >
>> >
>> >
>> >
>> > On 19 Jan 2021 23:45, Miluji Sb <[hidden email]> wrote:
>> >
>> > Dear all,
>> >
>> > I have more than 200 text files in a folder without header - example
>> > below.
>> > I would like to read, add a common date header to all the files, and
>> write
>> > (replace) the files.
>> >
>> > ## Read files
>> > filelist = list.files(pattern = ".*.txt")
>> > datalist = lapply(filelist, function(x)read.table(x, header=F))
>> >
>> > ## What I want to add
>> > date <- 20000101
>> > datalist _new <- lapply(datalist, function(x) rbind(x, date))
>> >
>> > How do I add this date as a common header and replace the files? Any
>> help
>> > will be highly appreciated. Thank you.
>> >
>> > Best,
>> >
>> > Milu
>> >
>> > ## Sample data
>> > xy <- dput(head(x,6))
>> > structure(list(V1 = c("-5.28082885742185,-0.509039307",
>> > "-6.09873046874998,-0.349584961",
>> > "-2.07150878906248,6.264276123", "-1.11102905273435,6.365716553",
>> > "2.37749633789065,14.57106934", "4.9619079589844,18.91350708"
>> > )), row.names = c(NA, 6L), class = "data.frame")
>> >
>> > [[alternative HTML version deleted]]
>> >
>> > ______________________________________________
>> > [hidden email] mailing list -- To UNSUBSCRIBE and more, see
>> > 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.
>> >
>> >
>> >
>>
>>         [[alternative HTML version deleted]]
>>
>> ______________________________________________
>> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
>> 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.
>>
>

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.