[R] iterative read - write

Bert Gunter bgunter@4567 @end|ng |rom gm@||@com
Tue Apr 17 14:00:07 CEST 2018


Suggestions?

Yes. Don't do this.

Instead, wrap your code in a function, use lapply() to apply it over your
list of files, returning all results in one list, and then save the list in
an .rdata file.

Also, in future, please post in plain text, as html tends to get mangled on
this plain text list .

Bert

On Tue, Apr 17, 2018, 4:20 AM Neotropical bat risk assessments <
neotropical.bats using gmail.com> wrote:

> Hi all,
>
> I would like to set up an iterative read & write sequence to avoid
> reading and writing each file one at a time.
> Hundreds of data sets to re-calculate.  The code I have works well
> individually, but would like to set up an iterative read, calculate and
> write changing the input and output file names each iteration.
>
> I  think I have read that there is an R  feature using "Pipes" and %
> that may help but have not figured out how to do this.
> What I am seeking is a way to read a list of TXT files, calculate the
> values and write the results in an iterative manner to avoid manually
> reading each file individually
>
> Suggestions?
>
> Tnx,
> Bruce*
> *
>
> *# This works*
>
> *#read in a data file
> *
>
> *>Corbre_2 <- read.table("E:/Acoustic Parameters/~Emballonuridae/Data by
> genus/~Corbre/Corbre_2.txt",header=T,sep="\t",quote="")*
>
> *# create and calculate a new variable for time between call pusles
> *
>
> *Corbre_2 [c("TBC")] <- recode.variables(Corbre_2[c("TBC")] , "0 -> NA;")*
>
> *# Create new BW variable for band width then change data file name*
>
> *BW<-within(Corbre_2,BW<-Fmax-Fmin)*
>
> *# Create 2 new variables for fundamental H1 and third harmonic H3
> frequencies
> *
>
> *#based on Fc that is generally loudest at the 2^nd harmonic.*
>
> *BW<-within(BW,FcH1<-Fc*.5)*
>
> *BW<-within(BW,FcH3<-FcH1*3)*
>
> *#rename to a DF to calculate the stats
> *
>
> *BatStats<-BW*
>
> *BatStats<-Deducer::descriptive.table (vars = d
> (Dur,TBC,Fmin,Fmax,BW,Fmean,Fk,FcH1,Fc,FcH3,Sc,Pmc),data=
> BatStats,func.names =c("Valid N","Minimum","Maximum","Mean","St.
> Deviation"),func.additional= list(p10=function(x) quantile(x, c(0.10),
> na.rm=TRUE),p25=function(x) quantile(x, c(0.25),
> na.rm=TRUE),p75=function(x) quantile(x,
> c(0.75),na.rm=TRUE),p90=function(x) quantile(x, c(0.90),na.rm=TRUE)))*
>
> *## Write the results and change output name and directory for the
> calculated stats.*
>
> *write.csv(BatStats,file="C:\\=Bat data working\\Acoustic
> Parameters\\Corbre_Stats.csv")*
>
>
>
> --
> Bruce W. Miller, PhD.
> Neotropical bat risk assessments
> Conservation Fellow - Wildlife Conservation Society
>
> If we lose the bats, we may lose much of the tropical vegetation and the
> lungs of the planet
>
> Using acoustic sampling to identify and map species distributions for >25
> years.
>
> Providing free Interactive identification keys and call fact sheets for
> the vocal signatures of New World Bats
>
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help using r-project.org 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.
>

On Apr 17, 2018 4:20 AM, "Neotropical bat risk assessments" <
neotropical.bats using gmail.com> wrote:

Hi all,

I would like to set up an iterative read & write sequence to avoid
reading and writing each file one at a time.
Hundreds of data sets to re-calculate.  The code I have works well
individually, but would like to set up an iterative read, calculate and
write changing the input and output file names each iteration.

I  think I have read that there is an R  feature using "Pipes" and %
that may help but have not figured out how to do this.
What I am seeking is a way to read a list of TXT files, calculate the
values and write the results in an iterative manner to avoid manually
reading each file individually

Suggestions?

Tnx,
Bruce*
*

*# This works*

*#read in a data file
*

*>Corbre_2 <- read.table("E:/Acoustic Parameters/~Emballonuridae/Data by
genus/~Corbre/Corbre_2.txt",header=T,sep="\t",quote="")*

*# create and calculate a new variable for time between call pusles
*

*Corbre_2 [c("TBC")] <- recode.variables(Corbre_2[c("TBC")] , "0 -> NA;")*

*# Create new BW variable for band width then change data file name*

*BW<-within(Corbre_2,BW<-Fmax-Fmin)*

*# Create 2 new variables for fundamental H1 and third harmonic H3
frequencies
*

*#based on Fc that is generally loudest at the 2^nd harmonic.*

*BW<-within(BW,FcH1<-Fc*.5)*

*BW<-within(BW,FcH3<-FcH1*3)*

*#rename to a DF to calculate the stats
*

*BatStats<-BW*

*BatStats<-Deducer::descriptive.table (vars = d
(Dur,TBC,Fmin,Fmax,BW,Fmean,Fk,FcH1,Fc,FcH3,Sc,Pmc),data=
BatStats,func.names =c("Valid N","Minimum","Maximum","Mean","St.
Deviation"),func.additional= list(p10=function(x) quantile(x, c(0.10),
na.rm=TRUE),p25=function(x) quantile(x, c(0.25),
na.rm=TRUE),p75=function(x) quantile(x,
c(0.75),na.rm=TRUE),p90=function(x) quantile(x, c(0.90),na.rm=TRUE)))*

*## Write the results and change output name and directory for the
calculated stats.*

*write.csv(BatStats,file="C:\\=Bat data working\\Acoustic
Parameters\\Corbre_Stats.csv")*



-- 
Bruce W. Miller, PhD.
Neotropical bat risk assessments
Conservation Fellow - Wildlife Conservation Society

If we lose the bats, we may lose much of the tropical vegetation and the
lungs of the planet

Using acoustic sampling to identify and map species distributions for >25
years.

Providing free Interactive identification keys and call fact sheets for the
vocal signatures of New World Bats


        [[alternative HTML version deleted]]

______________________________________________
R-help using r-project.org 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]]




More information about the R-help mailing list