Sean Hoang
Sean Hoang

Reputation: 3

Loop set a list of command for different variables in R?

I've just studied in R programming. I'm trying to create an automatic calculation for my data, but as a newcomer, i've found it so much more difficult than i though. I hope someone help me figure this out.

My data consist of dataset of 11 species in different Csv files in the same folder. I need to run a set of command to get the graphs. For each species, i need to type again commands (From glm...to the end).

>setwd("C:\Users\OneDrive\Work\Journal Articles\I9-2019\Reports\LM50")     
>library(FSA)
>temp = list.files(pattern="*.csv")
                for (i in 1:length(temp)) assign(temp[i], read.table(temp[i],header=T,sep=";"))
>lr<-function(cf,p)(log(p/(1-p))-cf[1])/cf[2]
>glmFencr<-glm(Maturity~FL,data=`Encrasicholina heteroloba F 2019 TNB I9.csv`,family=binomial)
>(L50Fencr<-lr(coef(glmFencr),0.5))
> '**First result**
>fitPlot(glmFencr,xlab="FL (mm)",ylab="Percentage (%)",main="",xlim=c(0,180),plot.p=FALSE)
>lines(c(0,L50Fencr),c(0.5,0.5),lty=3,lwd=2,col="black")
>lines(c(L50Fencr,L50Fencr),c(-0.2,0.5),lty=3,lwd=2,col="black")
>**second result (graph)**

So does it possible to create a loopset for this one?

Thank you in advance!~

Upvotes: 0

Views: 230

Answers (2)

Allan Cameron
Allan Cameron

Reputation: 174586

You could simply store your data frames in a list and loop through them. Notice it helps a lot to have your code indented and properly spaced.

library(FSA)
setwd("C:\Users\OneDrive\Work\Journal Articles\I9-2019\Reports\LM50")     

lr <- function(cf, p)
{
  return((log(p/(1-p))-cf[1])/cf[2])
}

all_files <- list.files(pattern="*.csv")

# Use lapply to get all your data frames and models into lists
all_data <- lapply(all_files, function(x) read.table(x, header = T, sep = ";"))
all_models <- lapply(all_data, function(x) glm(Maturity ~ FL, data = x, family = binomial))

# Now you can get your L50s as a vector
all_L50s <- unlist(lapply(all_models, function(x) lr(coef(x), 0.5)))

# Now you can loop quite easily
for(i in seq_along(all_models))
{
  fitPlot(all_models[[i]], xlim=c(0, 180), plot.p = FALSE,
          xlab = "FL (mm)", ylab = "Percentage(%)", main = "")
  lines(c(0, all_L50s[i]), c(0.5, 0.5), lty = 3, lwd = 2, col = "black")
  lines(c(all_L50s[i], all_L50s[i]), c(-0.2, 0.5), lty = 3, lwd = 2, col = "black")
}

Upvotes: 1

Ronak Shah
Ronak Shah

Reputation: 389325

Put the code that you want to apply to each file in a function

library(FSA)

lr <- function(cf,p)(log(p/(1-p))-cf[1])/cf[2]

apply_fun <- function(data) {
   glmFencr <- glm(Maturity~FL,data=data,family=binomial)
   L50Fencr <- lr(coef(glmFencr),0.5)
   fitPlot(glmFencr,xlab="FL (mm)", ylab="Percentage (%)",
                    main="", xlim=c(0,180), plot.p=FALSE)
   lines(c(0,L50Fencr),c(0.5,0.5),lty=3,lwd=2,col="black")
   lines(c(L50Fencr,L50Fencr),c(-0.2,0.5),lty=3,lwd=2,col="black")
}

Get all the files and apply the function to each file using lapply. Store the output in out_plot.

temp = list.files(pattern="*.csv")
out_plot <- lapply(temp, function(x) {
   df <- read.table(x, header=T,sep=";")
   apply_fun(df)
})

Upvotes: 1

Related Questions