Loop in R not functioning correctly - r

I have problems with my coded loop for some of my variables. I have run it firstly with one variable and it ran fine. Upon entering a different variable and different data range, the loop did not include all the outputs that it should have done according to the loop. There was no error message. The code for the loop is below:
geartype=data[179:187]
data_stats_structure_gt=as.data.frame(matrix(nrow=0,ncol=3))
names(data_stats_structure_gt)=c('Gear.Type','Man_Kendall_pvalue','Man_Kendall_tauv')
for (i in 1:ncol(geartype)){
dat=geartype[,i]
dat=as.data.frame(cbind(data$ï..Year,dat))
ds=names(geartype)[i]
names(dat)=c("Year",names(geartype)[i])
dat=dat[-which(is.na(dat[,2])==TRUE),]
if(nrow(dat)>=3){
output=as.data.frame(matrix(nrow=1,ncol=3))
names(output)=c('Gear.Type','Man_Kendall_pvalue','Man_Kendall_tauv')
output$Gear.Type=names(geartype)[i]
output$Man_Kendall_pvalue=mk.test(dat[,2])$p.value
output$Man_Kendall_tauv=mk.test(dat[,2])$estimates[3]
data_stats_structure_gt=rbind(data_stats_structure_gt,output)
}
}

Related

Levels for several variables with for loop (R)

I want to see the level as an output of several variables.
Let's say I have 9 variables, and I could do it like this:
levels(myd$tel_1)
levels(myd$tel_2)
levels(myd$tel_3)
...
But I want to improve it by using a for loop. I think it's useless to use the same function instead of the same code copied underneath.
I've tried something like this:
block <-
c("tel_1",
"tel_2",
"tel_3",
"tel_4",
"tel_5",
"tel_6",
"tel_7",
"tel_8",
"tel_9")
for (j in block) {
levels(myd[[j]])
}
But this does not work, no error message, nothing. Only the code copied into the console.
Thanks for help!

Loop program with commands to other programs

I have these lines of code in one program:
source("R:/ML NC8 MENSAL.R")
source("R:/ ML NPC NC8 MENSAL.R")
The mentioned programs both have these lines of code:
# Defining Variable
MONTH <- "01_2021"
I want to make this definition in the first program for the two programs.
Which code should I write?
Thank you for your help.
If both scripts have these lines and you only want to return it from the first script, then I would write a function in both scripts. Only the first one would return the month-value.
month_return_scr1 <- function(){
MONTH <- "01_2021"
#more code
return(list(MONTH, more variables, or data.frames)}
month_return_scr2 <- function(){
MONTH <- "01_2021"
#more code
return(list(more variables, or data.frames)}
The Month would then not be returned by the second source.
I used successfully the following solution:
Create a program - R:/constants.R - with the month variable (and any
others used in all programs)
Create a program - R:/superprogram.R - that executes all the 23 programs
In each 23 programs replace the variables definition for this code
source("R:/constants.R"). This will bring the constants defined in
the source file into the global environment.
Change the variables in program R:/constants.R and save it
Run R:/superprogram.R

Is there a way to check where R is 'stuck' within a for loop? (R)

I am using system() to run several files iteratively through a program via CMD. It deposits each outputs into a sub-directory designated for specifically and only that input file. So # of inputs is exactly equal to the number of output directories/outputs.
My code works for the first iteration, but I can see in the console that it won't move on to the second file after completing the first. The stop sign remains active so I know R is still 'running', but since the for loop environment is unique I can't really tell what it's stuck on. It just stays like this for hours. Therefore I'm not sure how to begin to diagnose the issue I'm having. Is there a way of tracing what happened after cancelling the code, for example?
If your curious, the code looks like this btw. I don't know how to make it reproducible, so I just commented each line:
for (i in 1:length(flist)) {
##flist is a vector of character strings. Each
row of characters is both the name of the input file and the name of the
output directory
setwd(paste0(solutions_dir, "\\", flist[i]))
#sets the appropriate dir
system(paste0(program_dir,"\\program.exe I=",
file_dir, "\\", flist[i], " O=",solutions_dir, "\\", flist[i],
"\\solv"))
##line that inputs program's exe file and the appropriate input/output
locations
}

How to automatically skip over errors in r

I'm trying to create new variables from some output from a number of two-piece segmented regression models that I'm running. The code for my new variable is:
initial1=c(fmod$psi[1],fmod2$psi[1], fmod3$psi[1], fmod4$psi[1], fmod5$psi[1], fmod6$psi[1], fmod7$psi[1], fmod8$psi[1], fmod9$psi[1], fmod10$psi[1], fmod11$psi[1],fmod12$psi[1], fmod13$psi[1], fmod14$psi[1], fmod15$psi[1], fmod16$psi[1], fmod17$psi[1], fmod18$psi[1], fmod19$psi[1], fmod20$psi[1], fmod21$psi[1],fmod22$psi[1], fmod23$psi[1], fmod24$psi[1], fmod25$psi[1], fmod26$psi[1], fmod27$psi[1], fmod28$psi[1], fmod29$psi[1], fmod30$psi[1], fmod31$psi[1],fmod32$psi[1], fmod33$psi[1], fmod34$psi[1], fmod35$psi[1], fmod36$psi[1], fmod37$psi[1], fmod38$psi[1], fmod39$psi[1], fmod40$psi[1], fmod41$psi[1],fmod42$psi[1], fmod43$psi[1], fmod44$psi[1], fmod45$psi[1], fmod46$psi[1], fmod47$psi[1], fmod48$psi[1], fmod49$psi[1], fmod50$psi[1], fmod51$psi[1],fmod52$psi[1], fmod53$psi[1], fmod54$psi[1], fmod55$psi[1], fmod56$psi[1], fmod57$psi[1], fmod58$psi[1], fmod59$psi[1], fmod60$psi[1], fmod61$psi[1], fmod62$psi[1], fmod63$psi[1], fmod64$psi[1])
where fmod, fmod2, fmod3, etc. are my regression models. Some of the regression models have errors and do not produce output (because the initial breakpoint estimates are too close to each other). Because of that, when I try to make my 'initial1' variable, I get error messages such as:
Error: object 'fmod12' not found
and the 'initial' variable is not created. I would like these models that don't have output associated with them to be automatically skipped over, or to be replaced with 'NA'. Does anyone know how to do this?
You're creating many different models and giving them numbered names. Why not put them in a list instead?
At model creation time:
for (i in 1:lots) fmod[[i]] <- my_segmented_reg(...)
where my_segmented_reg presumably returns either a model, or NULL or NA.
Then you have a list fmod which you can start using straight away.

while clause looping in R

I need help on the following code
I tried to generate a random series using while clause and met the following problem
When run codes as below
m1=0.82
set=rep(1, 1000)
while(mean(set)!=m1){
k=sample(1:1000,1)
if(set[k]!=0){set[k]=0}
}
every thing is fine, however the loop can end if I change ml as as below
m1=1-0.18
set=rep(1, 1000)
while(mean(set)!=m1){
k=sample(1:1000,1)
if(set[k]!=0){set[k]=0}
}
just change from ml=0.82 to ml=1-0.18, R can't stop looping.

Resources