Search code examples
rloopstime-complexitysystemtime

Build a dataframe conditioned to time


I am trying to build a data frame named 'df' that registers the time stamp when each row was entered. 'df' should have a unique column:

The data I want to input in 'df' is from data frame 'a', column 'textid':

str(a$textid)

chr [1:262] "xxxxx yyy" ...

'a' is composed as:

str(a)

'data.frame': 262 obs. of 3 variables: $ V1 : chr "Refierenos alguien que compre o arriende, si concreta obtén un ingreso extra \n\ngoo.gl/OlPYuZ" "Menciona a un amigo que quiera comprar una propiedad, si concreta, consigue dinero plus\n\ngoo.gl/OlPYuZ" "Refierenos alguien que compre o arriende, si concreta obtén un ingreso extra \n\ngoo.gl/OlPYuZ" "Menciona a un amigo que quiera comprar una propiedad, si concreta, consigue dinero plus\n\ngoo.gl/OlPYuZ" ... $ textid: chr "xxxxx yyyy" ... $ limit : logi FALSE FALSE FALSE FALSE FALSE FALSE ...

dput(droplevels(head(a)))

structure(list(V1 = c("Refierenos alguien que compre o arriende, si concreta obtén un ingreso extra \n\ngoo.gl/OlPYuZ", "Menciona a un amigo que quiera comprar una propiedad, si concreta, consigue dinero plus\n\ngoo.gl/OlPYuZ", "Refierenos alguien que compre o arriende, si concreta obtén un ingreso extra \n\ngoo.gl/OlPYuZ", "Menciona a un amigo que quiera comprar una propiedad, si concreta, consigue dinero plus\n\ngoo.gl/OlPYuZ", "Refierenos alguien que compre o arriende, si concreta obtén un ingreso extra \n\ngoo.gl/OlPYuZ", "Menciona a un amigo que quiera comprar una propiedad, si concreta, consigue dinero plus\n\ngoo.gl/OlPYuZ" ), textid = c("xxxxx yyy", "xxxxx yyy", "xxxxx yyy", "xxxxx yyy", "xxxxx yyy", "xxxxx yyy" ), limit = c(FALSE, FALSE, FALSE, FALSE, FALSE, FALSE)), .Names = c("V1", "textid", "limit"), row.names = c(NA, 6L), class = "data.frame")

I need a condition for time. Every row has to be input if:

if(as.integer(Sys.time()) %% 11 & as.integer(Sys.time()) %% 17 != 0)

After the row is entered there should be a loop that should wait for the next time that Sys.Time() (coerced as an integer value) matches that condition.

For that I have built this code:

df <- c(NA)
df <- as.data.frame(matrix(c(a), nrow = nrow(a)))


i=1

while(i <= nrow(a)) {
    repeat {
        if (as.integer(Sys.time()) %% 11 & as.integer(Sys.time()) %% 17 != 0) {
            break
        } else {
            df[i,]<- paste(Sys.time(),a$textid[i])
            i=i+1
        }
    }
}

Unsuccesfully I am obtaining al df's rows at the same time with the same time stamp.

str(df)

'data.frame': 2982 obs. of 1 variable: $ c(NA): chr "2017-07-10 13:14:58 xxxxx yyy" ...

Then I have tried

i=1 
while(i<=nrow(ids) & as.integer(Sys.time()) %% 11 == 0 & as.integer(Sys.time()) %% 17 == 0) {
    df[i,]<-paste(Sys.time(),a$textid[i]) 
    i=1+i
}

but I get an empty 'df'.

Finnally I am trying:

i=1
df<-as.data.frame(c(NA))
repeat{
    if(as.integer(Sys.time()) %% 11 & as.integer(Sys.time()) %% 17 == 0{
        df[i,]<-paste(Sys.time(),a$textid[i])
        i=1+i
    }
  if(i>nrow(ids)){
    break
  }
}

But 'a's´ rows keep entering at the same time to 'df' and do not loop looking for the next condition in time that matches before entering each row.

dput(droplevels(head(df)))

structure(list(c(NA) = c("2017-07-11 16:30:46 xxxx yyyy", "2017-07-11 16:30:46 xxxxx yyy", "2017-07-11 16:30:46 xxxxx yyy", "2017-07-11 16:30:46 xxxxx yyy", "2017-07-11 16:30:46 xxxxx yyy", "2017-07-11 16:30:46 xxxxx yyy" )), .Names = "c(NA)", row.names = c(NA, 6L), class = "data.frame")

As you can see the time is the same for every row. What I am trying to get is something like:

structure(list(c(NA) = c("2017-07-11 16:30:46 xxxx yyyy", "2017-07-11 16:31:12 xxxxx yyy", "2017-07-11 16:31:51 xxxxx yyy", "2017-07-11 16:33:33 xxxxx yyy", "2017-07-11 16:33:35 xxxxx yyy", "2017-07-11 16:36:28 xxxxx yyy" )), .Names = "c(NA)", row.names = c(NA, 6L), class = "data.frame")


Solution

  • I do not quite get what you really need. Here are my two guesses:

    1. You want to block code execution for each row insertion until your time criterion is met again, so you can do some time critical code execution. Then you need Sys.sleep() to halt your code execution.

      a <- structure(list(V1 = c("Refierenos alguien que compre o arriende, si concreta obtén un ingreso extra \n\ngoo.gl/OlPYuZ", "Menciona a un amigo que quiera comprar una propiedad, si concreta, consigue dinero plus\n\ngoo.gl/OlPYuZ", "Refierenos alguien que compre o arriende, si concreta obtén un ingreso extra \n\ngoo.gl/OlPYuZ", "Menciona a un amigo que quiera comprar una propiedad, si concreta, consigue dinero plus\n\ngoo.gl/OlPYuZ", "Refierenos alguien que compre o arriende, si concreta obtén un ingreso extra \n\ngoo.gl/OlPYuZ", "Menciona a un amigo que quiera comprar una propiedad, si concreta, consigue dinero plus\n\ngoo.gl/OlPYuZ" ), textid = c("xxxxx yyy", "xxxxx yyy", "xxxxx yyy", "xxxxx yyy", "xxxxx yyy", "xxxxx yyy" ), limit = c(FALSE, FALSE, FALSE, FALSE, FALSE, FALSE)), .Names = c("V1", "textid", "limit"), row.names = c(NA, 6L), class = "data.frame")
      
      #Using your example code:
      df <- data.frame(V1 = rep(NA, nrow(a)))
      i <- 1
      while(i <= nrow(a)) {
          if (as.integer(Sys.time()) %% 11 & as.integer(Sys.time()) %% 17 != 0) {
              #Sleep to reduce CPU occupation.
              Sys.sleep(0.5)
          } else {
              df[i,]<- paste(Sys.time(),a$textid[i])
              ###############################
              # Do something time critical. #
              ###############################
              i <- i + 1
              #Blocking for 1 second, thus as.integer(Sys.time()) will
              #be garantied to be different.
              Sys.sleep(1)
          }
      }
      
      df
      #                             V1
      #1 2017-07-13 02:43:48 xxxxx yyy
      #2 2017-07-13 02:43:54 xxxxx yyy
      #3 2017-07-13 02:43:59 xxxxx yyy
      #4 2017-07-13 02:44:10 xxxxx yyy
      #5 2017-07-13 02:44:11 xxxxx yyy
      #6 2017-07-13 02:44:21 xxxxx yyy
      
    2. You simply need the filled data frame starting from the current system time.

      a <- structure(list(V1 = c("Refierenos alguien que compre o arriende, si concreta obtén un ingreso extra \n\ngoo.gl/OlPYuZ", "Menciona a un amigo que quiera comprar una propiedad, si concreta, consigue dinero plus\n\ngoo.gl/OlPYuZ", "Refierenos alguien que compre o arriende, si concreta obtén un ingreso extra \n\ngoo.gl/OlPYuZ", "Menciona a un amigo que quiera comprar una propiedad, si concreta, consigue dinero plus\n\ngoo.gl/OlPYuZ", "Refierenos alguien que compre o arriende, si concreta obtén un ingreso extra \n\ngoo.gl/OlPYuZ", "Menciona a un amigo que quiera comprar una propiedad, si concreta, consigue dinero plus\n\ngoo.gl/OlPYuZ" ), textid = c("xxxxx yyy", "xxxxx yyy", "xxxxx yyy", "xxxxx yyy", "xxxxx yyy", "xxxxx yyy" ), limit = c(FALSE, FALSE, FALSE, FALSE, FALSE, FALSE)), .Names = c("V1", "textid", "limit"), row.names = c(NA, 6L), class = "data.frame")
      
      #Using your example code:
      df <- data.frame(V1 = rep(NA, nrow(a)))
      i <- 1
      t <- Sys.time()
      while(i <= nrow(a)) {
          if (!(as.integer(t) %% 11 & as.integer(t) %% 17 != 0)) {
              df[i,]<- paste(t,a$textid[i])
              i <- i + 1
          }
          t <- t + 1
      }
      
      df
      #                             V1
      #1 2017-07-13 02:43:48 xxxxx yyy
      #2 2017-07-13 02:43:54 xxxxx yyy
      #3 2017-07-13 02:43:59 xxxxx yyy
      #4 2017-07-13 02:44:10 xxxxx yyy
      #5 2017-07-13 02:44:11 xxxxx yyy
      #6 2017-07-13 02:44:21 xxxxx yyy
      

    The output of both code snippets is the same, depending on the system time it got executed.