[R] Need help with an iteration

Neotropical bat risk assessments neotrop|c@|@b@t@ @end|ng |rom gm@||@com
Tue Aug 13 14:59:20 CEST 2019


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



More information about the R-help mailing list