Output multiple files

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

Output multiple files

Muhammad Rahiz
Hello,

I want the ouput to take the name of the original file. I have the following code - but the problem is that the output overrides the original file which I desire not.

So given an input file called File1.txt, I want the ouput to be called File1.out.txt, or something along this line. I'm working with many files, fyi.

out=c("file1.txt", "file2.txt", "file3.txt")
for (i in 1:3){               # where 3 is the number of files
file[[i]] <- file[[i]] * 3   # just a simple calculation to multiply the matrix by 3
write.table(file[[i]],out[i], row.names=FALSE, col.names=FALSE)
}

Thanks.

--
Muhammad Rahiz  |  Doctoral Student in Regional Climate Modeling
Climate Research Laboratory, School of Geography & the Environment
Oxford University Centre for the Environment
South Parks Road, Oxford, OX1 3QY, United Kingdom
Tel: +44 (0)1865-285194 Mobile: +44 (0)7854-625974
Email: [hidden email]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Output multiple files

David Winsemius

On Dec 7, 2009, at 12:42 PM, Muhammad Rahiz wrote:

> Hello,
>
> I want the ouput to take the name of the original file. I have the  
> following code - but the problem is that the output overrides the  
> original file which I desire not.
>
> So given an input file called File1.txt, I want the ouput to be  
> called File1.out.txt, or something along this line. I'm working with  
> many files, fyi.
>
> out=c("file1.txt", "file2.txt", "file3.txt")

Take a look at this:

 > paste(strsplit(out[1], "\\.")[[1]][1], "out.txt", sep=".")
[1] "file1.out.txt"

> for (i in 1:3){               # where 3 is the number of files
> file[[i]] <- file[[i]] * 3   # just a simple calculation to multiply  
> the matrix by 3
> write.table(file[[i]],out[i], row.names=FALSE, col.names=FALSE)

perhaps something like this (untested in absence of data) :

write.table(file[[i]],file=paste(strsplit(out[i], "\\.")[[1]][1],  
"out.txt", sep="."), row.names=FALSE, col.names=FALSE)
}

>
> Thanks.
>
> --
> Muhammad Rahiz  |  Doctoral Student in Regional Climate Modeling
> Climate Research Laboratory, School of Geography & the Environment
> Oxford University Centre for the Environment
> South Parks Road, Oxford, OX1 3QY, United Kingdom Tel: +44  
> (0)1865-285194 Mobile: +44 (0)7854-625974
> Email: [hidden email]
>
> ______________________________________________
> [hidden email] 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.

David Winsemius, MD
Heritage Laboratories
West Hartford, CT

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Output multiple files

Muhammad Rahiz
Code works fine. Thanks!

Muhammad Rahiz  |  Doctoral Student in Regional Climate Modeling
Climate Research Laboratory, School of Geography & the Environment
Oxford University Centre for the Environment, University of Oxford
South Parks Road, Oxford, OX1 3QY, United Kingdom
Tel: +44 (0)1865-285194 Mobile: +44 (0)7854-625974
Email: [hidden email]



David Winsemius wrote:

> On Dec 7, 2009, at 12:42 PM, Muhammad Rahiz wrote:
>
>  
>> Hello,
>>
>> I want the ouput to take the name of the original file. I have the  
>> following code - but the problem is that the output overrides the  
>> original file which I desire not.
>>
>> So given an input file called File1.txt, I want the ouput to be  
>> called File1.out.txt, or something along this line. I'm working with  
>> many files, fyi.
>>
>> out=c("file1.txt", "file2.txt", "file3.txt")
>>    
>
> Take a look at this:
>
>  > paste(strsplit(out[1], "\\.")[[1]][1], "out.txt", sep=".")
> [1] "file1.out.txt"
>
>  
>> for (i in 1:3){               # where 3 is the number of files
>> file[[i]] <- file[[i]] * 3   # just a simple calculation to multiply  
>> the matrix by 3
>> write.table(file[[i]],out[i], row.names=FALSE, col.names=FALSE)
>>    
>
> perhaps something like this (untested in absence of data) :
>
> write.table(file[[i]],file=paste(strsplit(out[i], "\\.")[[1]][1],  
> "out.txt", sep="."), row.names=FALSE, col.names=FALSE)
> }
>
>  
>> Thanks.
>>
>> --
>> Muhammad Rahiz  |  Doctoral Student in Regional Climate Modeling
>> Climate Research Laboratory, School of Geography & the Environment
>> Oxford University Centre for the Environment
>> South Parks Road, Oxford, OX1 3QY, United Kingdom Tel: +44  
>> (0)1865-285194 Mobile: +44 (0)7854-625974
>> Email: [hidden email]
>>
>> ______________________________________________
>> [hidden email] 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.
>>    
>
> David Winsemius, MD
> Heritage Laboratories
> West Hartford, CT
>
>

______________________________________________
[hidden email] 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.