从日期向量中获取值

时间:2018-02-08 00:24:23

标签: r

我有一个带有三百个日期元素的向量,格式如下。 "2017-01-01 00:00:00"。它们被传递给一个函数,但当我读取它的长度时,我只得到两个值

single_audience<-function(random){
  number_of_audiences<-c()
  vector<-c(random)
  i<-1
  for (i in 1:length(random)) {
     num_al<-runif(1)
  }
}
return(length(random)

single_audience(initial_Dates(300)) 
# this is the 300 element vector. but in RETURN 
# I only get one element instead of the 300

1 个答案:

答案 0 :(得分:0)

嗨输出应该看起来像日期格式&#34; yyyy-mm-dd hh:mm:ss&#34;

pasing参数中的函数如下

initialdates<-function(randomdates) {
    date_0<-"2017-01-01 00:00:00"
    print(length(randomdates)) 
    vector_dates_origen<-c(as.POSIXct(date_0))
    for( i in 1:length(randomdates)) {
      vector_dates_origen<-append(vector_dates_origen,as.POSIXct("2017-01-01 
                                                                 00:00:00")+(sum(randomdates[1:i])*1440*60))         }  
    return(vector_dates_origen)                                             
  }
  generador_imputed_dates<-function(n) {
    alpha<-.123
    beta<-2.77
    first_generator<-c(42*rbeta(n,alpha,beta))
    return(first_generator)
  }
  generador_imputed_dates(97)
  initialdates(generador_imputed_dates(97))

这里有三个都在行动。

 single_audience<- function(random) {
 number_of_audiences<- vector()
 for (i in 1:length(random)) {
   num_al<-runif(n = random, max=random, min = 1)
 }
 number_of_audiences<-c(number_of_audiences, num_al)
 return(number_of_audiences)
 }
 initialdates<-function(randomdates) {
 date_0<-"2017-01-01 00:00:00"
 vector_dates_origen<-c(as.POSIXct(date_0))
 for( i in 1:length(randomdates)) {
 vector_dates_origen<-append(vector_dates_origen,as.POSIXct("2017-01-01 00:00:00")+(sum(randomdates[1:i])*1440*60))         
  }  
 return(vector_dates_origen)                                             
  }
  generador_imputed_dates<-function(n) {
  alpha<-.123
  beta<-2.77
  first_generator<-c(42*rbeta(n,alpha,beta))
  return(first_generator)
  }
  generador_imputed_dates(5)
  initialdates(generador_imputed_dates(5))
  single_audience(initialdates(generador_imputed_dates(5)))

导致:

> generador_imputed_dates(5)
[1] 4.105877e-02 1.658842e+01 5.799319e-10 9.177945e-02 1.420147e-03
> initialdates(generador_imputed_dates(5))
[1] "2017-01-01 00:00:00 CST" "2017-01-01 00:40:44 CST"
[3] "2017-01-01 10:32:04 CST" "2017-01-01 12:32:43 CST"
[5] "2017-01-04 23:31:21 CST" "2017-01-08 02:58:05 CST"
> single_audience(initialdates(generador_imputed_dates(5)))
[1] 1185122659  194495905  484833954   74975272  563523256  186841354`