Seeing the parameter values in each iteration of a loop - r

I am using RStudio, I have a programm and I want to see the value of each parameter in each iteration. I am seeing only value of parameters on the last iteration.
Here is my R code:
k<-read.csv("D:\\Testc.csv")[,1:5]
window<-64
stelle<-""
spalte<-1
co<-0
r<-1
l<-(2/sqrt(window))
while(spalte < 3)
{
datalist<-matrix(k[,spalte])
while(r+window<=length(datalist[,1]))
{
m<-acf(as.numeric(datalist[r:(r+window),1]),lag.max=32,plot=FALSE)$acf[-1]
for(i in (1:32))
{
if(m[i]>l)
{
co<-co+1
}
}
if(co>5)
{
row<-as.character(r)
spalte<-as.character(spalte)
pos<-rbind(row,spalte)
stelle<-c(stelle,"-",pos)
}
r<-r+30
co<-0
}
spalte<-spalte+1
}
stelle
I want to see the value of co ,spalte, stelle on each iteration. On the left side of RStudio on Workspace tab I see values only for last iteration.

place the following code into your loop at the end:
print(paste("co=", co))
print(paste("spalte=", spalte))
print(paste("stelle=", stelle))

Related

R loop for fuction until special value is reached [duplicate]

Can you confirm if the next break cancels the inner for loop?
for (out in 1:n_old){
id_velho <- old_table_df$id[out]
for (in in 1:n)
{
id_novo <- new_table_df$ID[in]
if(id_velho==id_novo)
{
break
}else
if(in == n)
{
sold_df <- rbind(sold_df,old_table_df[out,])
}
}
}
Well, your code is not reproducible so we will never know for sure, but this is what help('break')says:
break breaks out of a for, while or
repeat loop; control is transferred to
the first statement outside the
inner-most loop.
So yes, break only breaks the current loop. You can also see it in action with e.g.:
for (i in 1:10)
{
for (j in 1:10)
{
for (k in 1:10)
{
cat(i," ",j," ",k,"\n")
if (k ==5) break
}
}
}
your break statement should break out of the for (in in 1:n).
Personally I am always wary with break statements and double check it by printing to the console to double check that I am in fact breaking out of the right loop. So before you test add the following statement, which will let you know if you break before it reaches the end. However, I have no idea how you are handling the variable n so I don't know if it would be helpful to you. Make a n some test value where you know before hand if it is supposed to break out or not before reaching n.
for (in in 1:n)
{
if (in == n) #add this statement
{
"sorry but the loop did not break"
}
id_novo <- new_table_df$ID[in]
if(id_velho==id_novo)
{
break
}
else if(in == n)
{
sold_df <- rbind(sold_df,old_table_df[out,])
}
}

Avoid loop in moving window in R

I have an example R script for spectrum calculation. I divide the signal into several blocks and do the calculation for each block.
spect=function(x,samplingfrequency=1,blocksize=2^12)
{
T=length(x)
blocks=trunc(T/blocksize)
localfreq=c() ; localspec=c()
for(i in 1:blocks){localfreq[[i]]=c() ; localspec[[i]]=c() }
for(i in 1:blocks)
{
time=c( (1+(i-1)*blocksize) : (i*blocksize) )
localspectrum=spectrum(x[time],plot=FALSE)
localfreq[[i]]=localspectrum$freq
localspec[[i]]=localspectrum$spec
}
averagespec=rep(0,(blocksize/2))
for(freq in 1:(blocksize/2))
{
for(block in 1:blocks)
{
averagespec[freq]=(averagespec[freq]+localspec[[block]][freq])
}
averagespec[freq]=averagespec[freq]/blocks
}
par(mar=c(5.1,5.1,2.5,1.5))
plot(c(1:(blocksize/2))/(blocksize/2)*samplingfrequency/2,averagespec,log="xy",t="l",xlab="frequency [Hz]",ylab="average spectrum [a.u.]",cex.lab=1.8,cex.axis=1.8)
abline(v=(samplingfrequency/2),col=2)
abline(v=(1/blocksize*samplingfrequency),col=4)
}
here x is your time series input. I don't use the spectrum function from R directly since the result is too noisy. I was wonder if I could somehow avoid those forloops in my script?

R Crowd simulation with individual behavior

When i execute a crowd simulation, all dots start going to the suggested metting point (reffer to the code). However the dots (people leaving the room) start to step over the other dots, something that shouldnt be happening according to my excersice.
Here is the code:
dimensionX=10
dimensionY=10
numberPeople=20
velocity=0.001
varianzavelocidad=runif(1, min=0, max=5)
x<-dimensionX*runif(numberPeople)
y<-dimensionY*runif(numberPeople)
plot(x,y,xlim=c(0,dimensionX),ylim=c(0,dimensionY))
for(i in 1:10000) {
for(j in 1:numberPeople) {
ang <- atan((y[j]-5)/(10-x[j]))
x[j]<-x[j]+velocity*varianzavelocidad*cos(ang)
y[j]<-y[j]-velocity*varianzavelocidad*sin(ang)
}
x[x>10]=10
plot(x,y,xlim=c(0,dimensionX),ylim=c(0,dimensionY))
}
My thoughts were that i should be working with an If()/Else() condition inside the J array, however am not sure how to read each J object and set a condition that if the dot/point tries to step over another dot/point it would compare a strength value between the dots trying to step over.
A clue of how i would like to make it work:
dimensionX=10
dimensionY=10
numberPeople=20
velocity=0.001
strength=runit(1) *******
varianzavelocidad=runif(1, min=0, max=5)
x<-dimensionX*runif(numberPeople)
y<-dimensionY*runif(numberPeople)
plot(x,y,xlim=c(0,dimensionX),ylim=c(0,dimensionY))
for(i in 1:10000) {
for(j in 1:numberPeople) {
ang <- atan((y[j]-5)/(10-x[j]))
x[j]<-x[j]+velocity*varianzavelocidad*cos(ang)
y[j]<-y[j]-velocity*varianzavelocidad*sin(ang)
}
if(X[j1] = X[J2] && Y[J1] = Y[J2]) {
//MAKE THE DOTS CHOOSE BETWEEN THE STRONGEST //using strength value
} else() { they keep on going }
x[x>10]=10
plot(x,y,xlim=c(0,dimensionX),ylim=c(0,dimensionY))
}
I have tried downloading foreach function but honestly havent been able to find a way to use it correctly. Any thoughts?

Using Count To Split A Checklist Into 2 Columns in X++ Fetch Method

Here is what I have so far, this is returning two columns, but each counter is stopping and then duplicating the same value over and over...
if(lLogisticsControlTable.APMJobTypeId)
select count (RecID) from jobTypeCheck where jobTypeCheck.APMJobTypeId == lLogisticsControlTable.APMJobTypeId;
{
counter = jobTypeCheck.RecId;
}
while select jobTypeCheck where jobTypeCheck.APMJobTypeId == lLogisticsControlTable.APMJobTypeId
{
counter1 = counter / 2;
halfCount1 = counter - counter1;
if(halfcount <= counter1)
{
halfCount++;
jobListCheck1 = jobTypeCheck.Name;
}
if (halfCount1 > halfCount)
{
halfCount1++;
jobListCheck2 = jobTypeCheck.Name;
}
element.execute(2);
}
}
As Michael Brown indicated, it's difficult to understand the problem with half of the code ;)
However, I would suggest that you call the element.execute(2) method on every second pass through the loop? That way jobListCheck1 would be on the left, and jobListCheck2 would be on the right hand side. Finally you would then need to check immediately outside of your loop if you had an odd number of jobTypeCheck elements, and call the element.execute(2) method one last time remembering to set the jobListCheck2 variable as empty beforehand.
Regards

How to print specific column in row wise using unix?

I have one input file which is given below.
Values,series,setupresultcode,nameofresultcode,resultcode
2,9184200,,serviceSetupResultempty,2001
11,9184200,0,successfulReleasedByService,2001
194,9184200,1,successfulDisconnectedByCallingParty,2001
101,9184200,2,successfulDisconnectByCalledParty,2001
2,9184201,0,successfulReleasedByService,2001
78,9184201,1,successfulDisconnectedByCallingParty,2001
32,9184201,2,successfulDisconnectByCalledParty,2001
4,9184202,0,successfulReleasedByService,2001
63,9184202,1,successfulDisconnectedByCallingParty,2001
37,9184202,2,successfulDisconnectByCalledParty,2001
I want output as given below:
Series,successfulReleasedByService,successfulDisconnectedByCallingParty,successfulDisconnectByCalledParty,serviceSetupResultempty
9184200,11,194,101,2
9184202,4,63,37,
Keep series as common print value of series.i.e. first column with respect to result code.i.e third(integer) or fourth(string) column in input file.
For example: the second column of the data has n number of series; take 9184200. That series having 4 setupresultcode (empty,0,1,2). Name of each result code is given in 4th column. I want to print if resultcode is 0; i.e. successfulReleasedByService then print value 11 with respect to series 9184200.
Something like this might work although I haven't tested it, regard it as some kind of pseudo code.
#!/bin/awk -f
BEGIN
{
number_of_series=0;
}
{
#This part will be executed for every line
if ($3 =="0" || $3 == "1" || $3 == "2")
{
for (i=1; i<=number_of_series; i++)
{
#If the series has already been added
if(seriesarray[i] == $2)
{
#Concat the results
seriesarray[$2]=seriesarray[$2]","$1;
}
#If it's a new series
else
{
number_of_series++;
seriesarray[$2]=$1;
}
}
}
}
END
{
#Iterate over the series and print the series id and the concatenated results
for (series in seriesarray)
{
print series, seriesarray[series];
}
}
This would yield something like
9184200,11,194,101
9184201,2,78,32
9184202,4,63,37

Resources