Script execution is not stopping for Replicate

I am trying to run the below code in rstudio. This is a part of code used in Disk Space prediction as mentioned in the link (Click Here)

When I run the below lines the execution is not stopping and I am seeing the STOP button for hours. No clue on whats going on...

f <- function(spaceleft) {
days <- 0
while(spaceleft > 0) {
days <- days + 1
spaceleft <- spaceleft - sample(dudelta, 1, replace=TRUE)
}
days
}

freespace <- totalspace - tail(usd, 1)

daysleft <- replicate(5, f(freespace))

That code takes no time to be run in my home laptop
it throwns an error regarding totalspace:

freespace <- totalspace - tail(usd, 1)

daysleft <- replicate(5, f(freespace))




Error: object 'totalspace' not found

You are running the two lines alone.. I can give you the complete code or you can try the data and complete code given in the hyperlink --> Link

I copy and paste your code, period. Minor cosmetics to write my answer

Your code:

f <- function(spaceleft) {
days <- 0
while(spaceleft > 0) {
days <- days + 1
spaceleft <- spaceleft - sample(dudelta, 1, replace=TRUE)
}
days
}

freespace <- totalspace - tail(usd, 1)

daysleft <- replicate(5, f(freespace))

run at Rstudio, all toguether:

Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.

> f <- function(spaceleft) {
+   days <- 0
+   while(spaceleft > 0) {
+     days <- days + 1
+     spaceleft <- spaceleft - sample(dudelta, 1, replace=TRUE)
+   }
+   days
+ }
> 
> freespace <- totalspace - tail(usd, 1)
Error: object 'totalspace' not found
> 
> daysleft <- replicate(5, f(freespace))
Error in f(freespace) : object 'freespace' not found

you missed something in your example code

cheers
Fer

1 Like

To help us help you, could you please prepare a minimal reproducible example (reprex) illustrating your issue? Please have a look at this guide, to see how to create one:

I don't know about others, but I don't download data or run unknown code off the internet. Not saying you're malicious, it's just my general rule. Ideally, your code would look like what's in your first post, but with some creation of small objects in the beginning. For example:

# Replace this with stuff that resembles your actual data
dudelta <- runif(100)
freespace <- 1e5

f <- function(spaceleft) {
  days <- 0
  while(spaceleft > 0) {
    days <- days + 1
    spaceleft <- spaceleft - sample(dudelta, 1, replace=TRUE)
  }
  days
}

daysleft <- replicate(5, f(freespace))

Every problem I've had with while loops came from bad assumptions on my part. In your case, dudelta might not have the kind of values you think it does.

1 Like

One small help please. Based on the below code finally I have got the df0day. But now how can I see the predicted date along with the real data...

duinfo <- read.table('D:\DS Data\diskexportm906.csv',
col.names=c('computer','fullname','instance','day','type','gb','fullspace','usd','free','freepercent'),
colClasses = c("character","character","character","Date","character","numeric","numeric","numeric","numeric","numeric"))

attach(duinfo)
totalspace <- fullspace
today <- tail(day, 1)

dudelta <- diff(usd)

f <- function(spaceleft) {
days <- 0
while(spaceleft > 0) {
days <- days + 1
spaceleft <- spaceleft - sample(dudelta, 1, replace=TRUE)
}
days
}

freespace <- totalspace - tail(usd, 1)
daysleft <- replicate(5000, f(freespace))

plot(daysleft)

df0day <- sort(daysleft + today)
df0ecdfunc <- ecdf(df0day)
df0prob <- df0ecdfunc(df0day)

plot(df0day, df0prob, xaxt='n', type='l')
axis.Date(1, df0day, at=seq(min(df0day), max(df0day), 'year'), format='%F')

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.