两个函数 - 一个很好,一个有错误:$运算符对原子向量无效

时间:2018-03-08 15:45:34

标签: r vector shiny

我的问题:

我有两个在相同逻辑上运行的函数。我已将错误指向以下代码行。在第一个示例中,代码运行时没有错误:StressQoQ <- ifelse(HPF$index ==0, 0, StressQoQ)但是,在第二个示例中,我收到错误“Error:monthly_HPF $ index $ operator对原子向量无效。”对于这行代码mStressMoM <- ifelse(monthly_HPF$index ==0, 0, mStressMoM)这与第一种情况相同,所以我很困惑为什么它说month_HPF中的索引向量是原子的,但它不适用于HPF data.frame。我很困惑为什么在两种不同的情况下对同一列变量的处理方式不同。

此函数有效:

Stress_Path <- function(delta= -0.00251, term= 20) {                     # Stress Path function will modify the cumulative base vector by 'delta' for 'term' quarters

  stress_index <- c()                                                    # Initialize a stress_index vector that will be populated within the Stress_Path function. 

  stress_index <- HPF$index_value + delta*(HPF$counter <= term)          # Use boolean algebra to simplify the code and avoid if else statements. 

  HPF$StressC <<- stress_index                                           # Global scoping so that HPF data.frame is updated outside of function. 

  stress_indexplus <- c(stress_index[2:18370], NA)

  StressQoQ <- (stress_indexplus / stress_index) - 1
  StressQoQ <- c(NA, StressQoQ[1:18369])
  StressQoQ <- ifelse(HPF$index ==0, 0, StressQoQ)                       # Global scoping so that StressQoQ is updated outside of function. 
  HPF$StressQoQ <<- StressQoQ                                            # Global scoping so that HPF data.frame is updated outside of function. 

  return(HPF)
} 

Stress_Path(delta = -0.00251, term = 20)

此功能错误:

# Monthly Interpolation from Quarter-level data. ----
months <- lapply(HPF$fdate, seq.Date, by = "month", length.out = 3)      # Create a vector of dates to interpolate
months <- data.frame(fdate = do.call(what = c, months))                  # convert to data.frame 

monthly_HPF <- left_join(x = months, y = HPF, by = "fdate")              # left join existing HPF data.frame with expanded months data.frame - this will result in NA values where new month rows are created - impt for spline interpolation. 
# monthly_HPF <- rbindlist(monthly_HPF)
# dfmonthly_HPF <- do.call(rbind.data.frame, monthly_HPF)

monthly_HPF$StressC <- na.spline(object = monthly_HPF$StressC)           # Use cubic interpolation via spline to obtain results for new monthly fields
monthly_HPF$StressQoQ <- na.spline(object = monthly_HPF$StressQoQ)
monthly_HPF$index_value <- na.spline(object = monthly_HPF$index_value)
monthly_HPF$BaseQoQ <- na.spline(object = monthly_HPF$BaseQoQ)

Monthly_Stress_Path  <- function(delta = -0.00251, term = 20){

  mstress_index <- c()

  mstress_index <- monthly_HPF$index_value + ((-0.00251)**(1/3))*(monthly_HPF$counter <= 20*3)

  monthly_HPF <- mstress_index

  mstress_indexplus <- c(mstress_index[2:2057440], NA)

  mStressMoM <- (mstress_indexplus / mstress_index) - 1
  mStressMoM <- c(NA, mStressMoM[1:2057439])
  mStressMoM <- ifelse(monthly_HPF$index ==0, 0, mStressMoM)
  monthly_HPF$mStressMoM <<- mStressMoM

  return(monthly_HPF)
}

Monthly_Stress_Path(-0.00251, 20)

数据:

dput(head(HPF, 25))
structure(list(region = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), path = c(1, 1, 1, 1, 
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1
), date = c(20140215, 20140515, 20140815, 20141115, 20150215, 
20150515, 20150815, 20151115, 20160215, 20160515, 20160815, 20161115, 
20170215, 20170515, 20170815, 20171115, 20180215, 20180515, 20180815, 
20181115, 20190215, 20190515, 20190815, 20191115, 20200215), 
    index_value = c(1, 1.033852765, 1.041697122, 1.038876363, 
    1.041043093, 1.060900982, 1.073728928, 1.075879441, 1.080898915, 
    1.10368893, 1.119240863, 1.122827602, 1.128639801, 1.15275796, 
    1.169021733, 1.172707492, 1.178666441, 1.203634882, 1.220348482, 
    1.223890323, 1.229770019, 1.255791539, 1.273560554, 1.278236959, 
    1.285508086), index = c(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 
    11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24), 
    counter = 1:25, BaseQoQ = c(0, 0.033852765, 0.00758749917354051, 
    -0.00270784947028013, 0.00208564760655761, 0.0190749923163842, 
    0.0120915582298895, 0.00200284535874973, 0.00466546139717505, 
    0.0210843166587877, 0.0140908661646175, 0.00320461762840418, 
    0.00517639483536669, 0.0213692260175751, 0.0141085757499344, 
    0.00315285755256367, 0.00508136004984272, 0.0211836361259394, 
    0.0138859385432799, 0.00290231933930496, 0.00480410367620832, 
    0.021159663675294, 0.0141496533844698, 0.00367191413499146, 
    0.00568840303732765), fdate = structure(c(16116, 16205, 16297, 
    16389, 16481, 16570, 16662, 16754, 16846, 16936, 17028, 17120, 
    17212, 17301, 17393, 17485, 17577, 17666, 17758, 17850, 17942, 
    18031, 18123, 18215, 18307), class = "Date"), StressC = c(0.99749, 
    1.031342765, 1.039187122, 1.036366363, 1.038533093, 1.058390982, 
    1.071218928, 1.073369441, 1.078388915, 1.10117893, 1.116730863, 
    1.120317602, 1.126129801, 1.15024796, 1.166511733, 1.170197492, 
    1.176156441, 1.201124882, 1.217838482, 1.221380323, 1.229770019, 
    1.255791539, 1.273560554, 1.278236959, 1.285508086), StressQoQ = c(0, 
    0.0339379492526242, 0.00760596502560418, -0.0027143898728953, 
    0.00209069888540969, 0.0191210941026796, 0.0121202336548254, 
    0.00200753827606026, 0.00467637125510434, 0.0211333913794913, 
    0.0141229845362187, 0.00321182042946733, 0.00518799221722843, 
    0.021416855302633, 0.0141393626118667, 0.00315964160130755, 
    0.00509225924746737, 0.021228843485116, 0.0139149560969629, 
    0.00290830110260876, 0.0068690282969297, 0.021159663675294, 
    0.0141496533844698, 0.00367191413499146, 0.00568840303732765
    )), .Names = c("region", "path", "date", "index_value", "index", 
"counter", "BaseQoQ", "fdate", "StressC", "StressQoQ"), row.names = c(NA, 
-25L), class = c("grouped_df", "tbl_df", "tbl", "data.frame"), vars = "region", drop = TRUE, indices = list(
    0:24), group_sizes = 25L, biggest_group_size = 25L, labels = structure(list(
    region = 1), row.names = c(NA, -1L), class = "data.frame", vars = "region", drop = TRUE, .Names = "region"))

非常感谢任何见解!

2 个答案:

答案 0 :(得分:2)

在第二个功能中,您将以前制作的monthly_HPF替换为:

monthly_HPF <- mstress_index

然后在

中使用monthly_HPF

mStressMoM <- ifelse(monthly_HPF$index ==0, 0, mStressMoM)

你的错误就在那里。 monthly_HPF实际上是mstress_index,它是一个向量:

 str(mstress_index)
 num [1:75] NaN NA NA NaN NA NA NaN NA NA NaN ...

因此不具有$

的子集

这是一个经典的data.frame与矩阵/向量问题。确保在两个函数中为对象提供常量类。

在您的其他功能中,您致电:

HPF$StressC <<- stress_index

而不是

monthly_HPF <- mstress_index

尝试: monthly_HPF$index <- mstress_index

您将拥有正在寻找的data.frame

答案 1 :(得分:1)

在您的函数 Monthly_Stress_Path 的开头,对象 monthly_HPF 是一个data.frame。 但是稍后您将它重新分配给 mstress_index 这是一个向量。

class(monthly_HPF)
[1] "data.frame"

class(mstress_index)
[1] "numeric"

您不能将$运算符用于向量。 相反,您可以将 mstress_index 转换为data.frame

monthly_HPF <- data.frame(col1 = mstress_index)