For Loop and If in R Programming - r

I think I have a simple question, but I can not figure out how to do it.
I just wanna plot all hist of an data input and check if all var are numerical.
plotting_histogram_data<-function(x) {
for(i in seq_len((ncol(x)))) {
if(is.numeric(x[,i])) {
hist(x[,i])
}
else{
print("Variable is not of the Typ numeric!")
}
}
}
I know that the problem is the index.
Thank and have nice day.

Related

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?

Seeing the parameter values in each iteration of a loop

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))

Title on chart for gvisAnnotatedTimeLine function

I am using the gvisAnnotatedTimeLine function from the googleVis package, and was wondering if there was a way of adding in a Title (not an annotation) into the output, as I can't see an argument for it in the function help file.
Thanks in advance
Here is a function that should include a title to the chart. The input is either an HTML string or a shiny.tag.
addGvisATLTitle <- function(gvisATL,title) {
if (!all(class(gvisATL) == c("gvis","list"))) {
stop('ERROR in addGvisATLTitle: Incorrect type, expect gvisAnnotatedTimeLine.')
}
if (class(title) == "character") {
gvisATL$html$chart['divChart'] <- paste(title,gvisATL$html$chart['divChart'],sep="")
} else if (class(title) == "shiny.tag") {
gvisATL$html$chart['divChart'] <- paste(as.character(title)[1],gvisATL$html$chart['divChart'],sep="")
} else {
stop('ERROR in addGvisATLTitle: Unknown title type.')
}
return(gvisATL)
}
you can test it out with
a <- data.frame(date=Sys.Date(),val=20)
b <- gvisAnnotatedTimeLine(a)
plot(addTitle(b,"<h1> My chart </h1>"))
plot(addTitle(b,h1("My chart")))
I have updated it to work with gvisMerge

1050: Cannot assign to a non-reference value

Hi my code as fallows what is wrong with it?
thank you and sorry for my bad english.
protected function belgelerDG_itemClickHandler(event:ListEvent):void
{
var durum:Boolean = false;
if(belgeicerikWindow==null){
belgeicerikWindow=new belgeicerik();
belgeicerikWindow.title=belgelerDG.selectedItem.belge;
belgeicerikWindow.open();
}
else{
durum=false;
for ( var i:int = NativeApplication.nativeApplication.openedWindows.length - 1; i >= 0; --i ) {
if(NativeApplication.nativeApplication.openedWindows[i].title.toString() == belgeicerikWindow.title=belgelerDG.selectedItem.belge){
belgeicerikWindow.orderToFront();
durum=true;
}
}
if(durum==false){
belgeicerikWindow=new belgeicerik();
belgeicerikWindow.title=belgelerDG.selectedItem.belge;
belgeicerikWindow.open();
}
}
}
I'm betting the problem lies with the if statement that starts with:
if(NativeApplication.nativeApplication.openedWindows[i].title.toString()
You are doing an assignment within the value you are trying to compare against:
== belgeicerikWindow.title=belgelerDG.selectedItem.belge)
If it isn't what is causing your problem, at least it is something you should fix to make things more legible. :)

Resources