Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R loop为循环定义的对象分配不同的日期,并避免eval(解析)_R - Fatal编程技术网

R loop为循环定义的对象分配不同的日期,并避免eval(解析)

R loop为循环定义的对象分配不同的日期,并避免eval(解析),r,R,新的编码在R。 我希望使这段代码更加高效,并避免使用eval(parse)。目前,我正在创建大量的值对象,我确信它们可以更好地在向量或矩阵中进行布局 输入:用户定义日期YYYYMM;示例201712 iEval_YYYYMM <- '201712' 我能够创建我喜欢的所有值对象,但我知道这不是好代码,我希望避免eval(解析)。 我当前的代码: iEval_Dt <- ymd(paste(iEval_YYYYMM,'01',sep='')) #reformat user input

新的编码在R。 我希望使这段代码更加高效,并避免使用eval(parse)。目前,我正在创建大量的值对象,我确信它们可以更好地在向量或矩阵中进行布局

输入:用户定义日期YYYYMM;示例201712

iEval_YYYYMM <- '201712'
我能够创建我喜欢的所有值对象,但我知道这不是好代码,我希望避免eval(解析)。 我当前的代码:

iEval_Dt <- ymd(paste(iEval_YYYYMM,'01',sep='')) #reformat user input to date

l.name = c('RP0','RP1') #lag name vector
l.val = c(0,1) #lag value (months) vector
rp = c('Y0','Y1','Y2') #Rolling period vector

i = 1
j = 1
for(i in 1:length(l.name)) {
  for(j in 1:length(rp)) {

  b <- paste(l.name[i],rp[j],sep='')

  assign(b,iEval_Dt %m-% months(11 + l.val[i] + (j-1)*12)) #begin date of RP
  assign(paste('A.',b,sep=''),format(eval(parse(text=b)),'%Y-%m')) #formatA
  assign(paste('B.',b,sep=''),format(eval(parse(text=b)),'%d-%b-%y')) #formatB
  }
}

iEval\u Dt这会让你走上一条更好的道路。不要试图将所有内容存储在单独的对象中—这会导致您将
粘贴到一起的
eval(parsing())
名称—这确实很难使用。只需将所有内容放在数据框中:

library(lubridate)
start = ymd(20171201)

lags = 0:1
years = 0:2

results = expand.grid(lags = lags, years = years)
results$date = start - months(results$lags) - years(results$years)

results$A = format(results$date, "%d-%b-%y")
results$B = format(results$date, "%Y-%m")

results
#   lags years       date         A       B
# 1    0     0 2017-12-01 01-Dec-17 2017-12
# 2    1     0 2017-11-01 01-Nov-17 2017-11
# 3    0     1 2016-12-01 01-Dec-16 2016-12
# 4    1     1 2016-11-01 01-Nov-16 2016-11
# 5    0     2 2015-12-01 01-Dec-15 2015-12
# 6    1     2 2015-11-01 01-Nov-15 2015-11
现在,您可以使用更有意义的
(结果,[lags==1&years==0])
来代替神秘的
a.RP1Y0
。如果您需要像
Y0
Y1
等标签,请将它们粘贴到新列中


我不认为我精确地复制了您的逻辑,但您应该能够根据此示例使其工作。

这应该会让您走上一条更好的道路。不要试图将所有内容存储在单独的对象中—这会导致您将
粘贴到一起的
eval(parsing())
名称—这确实很难使用。只需将所有内容放在数据框中:

library(lubridate)
start = ymd(20171201)

lags = 0:1
years = 0:2

results = expand.grid(lags = lags, years = years)
results$date = start - months(results$lags) - years(results$years)

results$A = format(results$date, "%d-%b-%y")
results$B = format(results$date, "%Y-%m")

results
#   lags years       date         A       B
# 1    0     0 2017-12-01 01-Dec-17 2017-12
# 2    1     0 2017-11-01 01-Nov-17 2017-11
# 3    0     1 2016-12-01 01-Dec-16 2016-12
# 4    1     1 2016-11-01 01-Nov-16 2016-11
# 5    0     2 2015-12-01 01-Dec-15 2015-12
# 6    1     2 2015-11-01 01-Nov-15 2015-11
现在,您可以使用更有意义的
(结果,[lags==1&years==0])
来代替神秘的
a.RP1Y0
。如果您需要像
Y0
Y1
等标签,请将它们粘贴到新列中


我不认为我精确地复制了您的逻辑,但您应该能够基于此示例使其正常工作。

我的完整代码供感兴趣的人或提出建议的人使用:

library(lubridate)
iEval_YYYYMM <- readline(prompt="Enter evaluation month YYYYMM: ")
lag  <- c(0,1,3)
rp <- 0:2

tDates  <- expand.grid(lag = lag, rp = rp)
tDates$ID <- paste('RP',tDates$lag,'Y',tDates$rp,sep = '')

tDates$sDt <- iEval_Dt - months(11 + tDates$lag + tDates$rp * 12)
tDates$eDt <- tDates$sDt + months(12) - days(1)

tDates$sDt.EQ <- format(tDates$sDt,'%Y-%m')
tDates$sDt.CQ <- format(tDates$sDt,'%d-%b-%y')

tDates$eDt.EQ <- format(tDates$eDt,'%Y-%m')
tDates$eDt.CQ <- format(tDates$eDt,'%d-%b-%y')

CQUpdate <- function(q) {
i <- 1
j <- 1
tmp <- q

for(i in 1:length(lag)) {
  for(j in 1:length(rp)) {
  # replace start dates for each rolling period
  find <- paste('<<s',tDates$ID[tDates$lag == lag[i] & tDates$rp == 
              rp[j]],'>>',sep='')
  replace <- tDates$sDt.CQ[tDates$lag == lag[i] & tDates$rp == rp[j]]
  tmp <- gsub(find,replace,tmp,fixed=TRUE)

  # replace end dates for each rolling period
  find <- paste('<<e',tDates$ID[tDates$lag == lag[i] & tDates$rp == 
              rp[j]],'>>',sep='')
  replace <- tDates$eDt.CQ[tDates$lag == lag[i] & tDates$rp == rp[j]]
  tmp <- gsub(find,replace,tmp,fixed=TRUE)

  # replace date for End of Month date of Evaluation Month (DD/MM/YYYY)
  tmp <- gsub('<<EOM>>',format(iEval_Dt + months(1) - 
             days(1),'%d/%m/%Y'),tmp,fixed=TRUE)

   }
 } 
tmp
}

#subtle change from prior - replaces with EQ formats
EQUpdate <- function(q) { 
i <- 1
j <- 1
tmp <- q

for(i in 1:length(lag)) {
  for(j in 1:length(rp)) {
  # replace start dates for each rolling period
  find <- paste('<<s',tDates$ID[tDates$lag == lag[i] & tDates$rp == 
              rp[j]],'>>',sep='')
  replace <- tDates$sDt.EQ[tDates$lag == lag[i] & tDates$rp == rp[j]]
  tmp <- gsub(find,replace,tmp,fixed=TRUE)

  # replace end dates for each rolling period
  find <- paste('<<e',tDates$ID[tDates$lag == lag[i] & tDates$rp == 
              rp[j]],'>>',sep='')
  replace <- tDates$eDt.EQ[tDates$lag == lag[i] & tDates$rp == rp[j]]
  tmp <- gsub(find,replace,tmp,fixed=TRUE)

  # replace Eval month year and month
  tmp <- gsub('<<YYYY>>',substr(iEval_YYYYMM,1,4),tmp,fixed=TRUE)
  tmp <- gsub('<<MM>>',substr(iEval_YYYYMM,5,6),tmp,fixed=TRUE)

   }
 } 
tmp
}
库(lubridate)

iEval_YYYYMM我的完整代码,供感兴趣的人或提出建议的人使用:

library(lubridate)
iEval_YYYYMM <- readline(prompt="Enter evaluation month YYYYMM: ")
lag  <- c(0,1,3)
rp <- 0:2

tDates  <- expand.grid(lag = lag, rp = rp)
tDates$ID <- paste('RP',tDates$lag,'Y',tDates$rp,sep = '')

tDates$sDt <- iEval_Dt - months(11 + tDates$lag + tDates$rp * 12)
tDates$eDt <- tDates$sDt + months(12) - days(1)

tDates$sDt.EQ <- format(tDates$sDt,'%Y-%m')
tDates$sDt.CQ <- format(tDates$sDt,'%d-%b-%y')

tDates$eDt.EQ <- format(tDates$eDt,'%Y-%m')
tDates$eDt.CQ <- format(tDates$eDt,'%d-%b-%y')

CQUpdate <- function(q) {
i <- 1
j <- 1
tmp <- q

for(i in 1:length(lag)) {
  for(j in 1:length(rp)) {
  # replace start dates for each rolling period
  find <- paste('<<s',tDates$ID[tDates$lag == lag[i] & tDates$rp == 
              rp[j]],'>>',sep='')
  replace <- tDates$sDt.CQ[tDates$lag == lag[i] & tDates$rp == rp[j]]
  tmp <- gsub(find,replace,tmp,fixed=TRUE)

  # replace end dates for each rolling period
  find <- paste('<<e',tDates$ID[tDates$lag == lag[i] & tDates$rp == 
              rp[j]],'>>',sep='')
  replace <- tDates$eDt.CQ[tDates$lag == lag[i] & tDates$rp == rp[j]]
  tmp <- gsub(find,replace,tmp,fixed=TRUE)

  # replace date for End of Month date of Evaluation Month (DD/MM/YYYY)
  tmp <- gsub('<<EOM>>',format(iEval_Dt + months(1) - 
             days(1),'%d/%m/%Y'),tmp,fixed=TRUE)

   }
 } 
tmp
}

#subtle change from prior - replaces with EQ formats
EQUpdate <- function(q) { 
i <- 1
j <- 1
tmp <- q

for(i in 1:length(lag)) {
  for(j in 1:length(rp)) {
  # replace start dates for each rolling period
  find <- paste('<<s',tDates$ID[tDates$lag == lag[i] & tDates$rp == 
              rp[j]],'>>',sep='')
  replace <- tDates$sDt.EQ[tDates$lag == lag[i] & tDates$rp == rp[j]]
  tmp <- gsub(find,replace,tmp,fixed=TRUE)

  # replace end dates for each rolling period
  find <- paste('<<e',tDates$ID[tDates$lag == lag[i] & tDates$rp == 
              rp[j]],'>>',sep='')
  replace <- tDates$eDt.EQ[tDates$lag == lag[i] & tDates$rp == rp[j]]
  tmp <- gsub(find,replace,tmp,fixed=TRUE)

  # replace Eval month year and month
  tmp <- gsub('<<YYYY>>',substr(iEval_YYYYMM,1,4),tmp,fixed=TRUE)
  tmp <- gsub('<<MM>>',substr(iEval_YYYYMM,5,6),tmp,fixed=TRUE)

   }
 } 
tmp
}
库(lubridate)

iEval_YYYYMM这工作很好,正是我想要的-谢谢!我创建了一个数据框tDates,然后调用这个数据框来查找/替换一个长字符对象(用于SQL查询)。这工作非常出色,正是我想要的-谢谢!我创建了一个数据帧tDates,然后调用这个数据帧来查找/替换长字符对象(用于SQL查询)。