Need help with an iteration

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

Need help with an iteration

Bat Dude
Hi all,

I am having an issue with trying to run iterations for the same analysis
with a list of data sets.  I am assuming I am missing a basic step here.

Code I am trying is below.  Not certain if a NEXT call needs to be
included somewhere or not.
This is run under the Deducer GUI.  I would also be content to simply
run in a basic R console if that works.

# First step, was to create a folder with the simple data files for the
analysis

# Set the WD for that directory

setwd("C://Embalonurids")

# Call all the names of the files in that folder

file.names <- dir(pattern = "*.txt")

#Make a LOOP for the analysis

for(i in 1:length(file.names)){file <-
read.table(file.names[i],header=TRUE) # read the text files one by one

   # Deducer code used

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

   # Create new BW variable and data set then change data file name for
the analysis

   BW<-within(file,BW<-Fmax-Fmin)

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

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

   BatStats<-BW
# In the Deducer GUI this is the code that runs manually if I run these
one by one
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(p75=function(x) quantile(x, c(0.75),
na.rm=TRUE),p85=function(x) quantile(x, c(0.85),
na.rm=TRUE),p90=function(x) quantile(x,
c(0.90),na.rm=TRUE),p99=function(x) quantile(x, c(0.99),na.rm=TRUE)))

  ## Then the results need to be written to an outputdirectory

  name <- as.character(paste(file.names[i], ".csv", sep="")) # this
creates an element that will have the name of the file that is used in
each run

# I have tried both setting a new WD or simply defined where to writ the
results

setwd("C://Embalonurids/Results")

write.csv(BatStats, name)    #this should assign the names of the files
as it iterates one by one })

#also tried directing where to write the outputas I do when manually
running this

#write.csv(BatStats,name,file="C:\\Emballonurids\Results"))


I would like to run this as an iteration as I have > 200 data files to
process and would love to have it run in background while I am working
on other things.

An assistance welcomed.

Bruce

--
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
and pioneering acoustic tools for ecology and conservation of bats for >25 years.

Key projects include providing free interactive identification keys and call fact sheets for the vocal signatures of New World Bats

______________________________________________
[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: Need help with an iteration

PIKAL Petr
Hi

I does not use Deducer so I do not know if it has some features for such task

In plain R instead
file.names <- dir(pattern = "*.txt")

 use
file.names <- list.files(pattern = "*.txt")

for(i in 1:length(file.names)){file <-
> read.table(file.names[i],header=TRUE) #
Here you want to change zeros to NA in TBC column

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

is.na(file[, "TBC"]) <- (file[,"TBC"]==0)

This is cryptic for me

# Create new BW variable and data set then change data file name for
the analysis

but if I understand correctly, you want to have some summary statistics on each file

For this you could try
?apply, ?summary, ?fivenum or use your own function - something like

myf <-function(x) c(mean(x), min(x), max(x), sd(x))

Store result in one row
result[i,] <- apply(file, 2,myf)

Output result after cycle ends.
}
write.csv(result, "somename.csv")

Data frame "result" could be predefined based on number of your files and output columns.

Cheers
Petr

> -----Original Message-----
> From: R-help <[hidden email]> On Behalf Of Neotropical bat
> risk assessments
> Sent: Tuesday, August 13, 2019 2:59 PM
> To: [hidden email]
> Subject: [R] Need help with an iteration
>
> Hi all,
>
> I am having an issue with trying to run iterations for the same analysis with a
> list of data sets.  I am assuming I am missing a basic step here.
>
> Code I am trying is below.  Not certain if a NEXT call needs to be included
> somewhere or not.
> This is run under the Deducer GUI.  I would also be content to simply run in a
> basic R console if that works.
>
> # First step, was to create a folder with the simple data files for the analysis
>
> # Set the WD for that directory
>
> setwd("C://Embalonurids")
>
> # Call all the names of the files in that folder
>
> file.names <- dir(pattern = "*.txt")
>
> #Make a LOOP for the analysis
>
> for(i in 1:length(file.names)){file <-
> read.table(file.names[i],header=TRUE) # read the text files one by one
>
>    # Deducer code used
>
>    file[c("TBC")] <- recode.variables(file[c("TBC")] , "0 -> NA;")


>
>    # Create new BW variable and data set then change data file name for the
> analysis
>
>    BW<-within(file,BW<-Fmax-Fmin)
>
>    BW<-within(BW,FcH1<-Fc*.5)
>
>    BW<-within(BW,FcH3<-FcH1*3)
>
>    BatStats<-BW
> # In the Deducer GUI this is the code that runs manually if I run these one by
> one 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(p75=function(x) quantile(x, c(0.75),
> na.rm=TRUE),p85=function(x) quantile(x, c(0.85),
> na.rm=TRUE),p90=function(x) quantile(x,
> c(0.90),na.rm=TRUE),p99=function(x) quantile(x, c(0.99),na.rm=TRUE)))
>
>   ## Then the results need to be written to an outputdirectory
>
>   name <- as.character(paste(file.names[i], ".csv", sep="")) # this creates an
> element that will have the name of the file that is used in each run
>
> # I have tried both setting a new WD or simply defined where to writ the
> results
>
> setwd("C://Embalonurids/Results")
>
> write.csv(BatStats, name)    #this should assign the names of the files as it
> iterates one by one })
>
> #also tried directing where to write the outputas I do when manually running
> this
>
> #write.csv(BatStats,name,file="C:\\Emballonurids\Results"))
>
>
> I would like to run this as an iteration as I have > 200 data files to process and
> would love to have it run in background while I am working on other things.
>
> An assistance welcomed.
>
> Bruce
>
> --
> 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 and
> pioneering acoustic tools for ecology and conservation of bats for >25 years.
>
> Key projects include providing free interactive identification keys and call fact
> sheets for the vocal signatures of New World Bats
>
> ______________________________________________
> [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.
Osobní údaje: Informace o zpracování a ochraně osobních údajů obchodních partnerů PRECHEZA a.s. jsou zveřejněny na: https://www.precheza.cz/zasady-ochrany-osobnich-udaju/ | Information about processing and protection of business partner’s personal data are available on website: https://www.precheza.cz/en/personal-data-protection-principles/
Důvěrnost: Tento e-mail a jakékoliv k němu připojené dokumenty jsou důvěrné a podléhají tomuto právně závaznému prohláąení o vyloučení odpovědnosti: https://www.precheza.cz/01-dovetek/ | This email and any documents attached to it may be confidential and are subject to the legally binding disclaimer: https://www.precheza.cz/en/01-disclaimer/

______________________________________________
[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.