Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/66.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
Q:特殊条件,R的变化率;ForLoop/应用/滞后_R_For Loop_If Statement_Rows - Fatal编程技术网

Q:特殊条件,R的变化率;ForLoop/应用/滞后

Q:特殊条件,R的变化率;ForLoop/应用/滞后,r,for-loop,if-statement,rows,R,For Loop,If Statement,Rows,我开始被R打湿了脚,我对时间序列的概念是全新的。谁能告诉我正确的方向来计算每月%的变化 我有不同年份、不同月份、不同城镇的数据&价格和变化率如是 在第二步中,我想在上表中取从9月开始到8月的所有可能对的平均比率->也就是说,9比10,9比11,…,9比8,10比11,…,10比8。。。7_8 计算: 变化率函数:a-b/b*100,其中a表示新月份,b表示上月 平均_比率:所有年份和城镇中各月份的平均值 %变化:log1+平均/x*100, 其中x是开始月份和结束月份之间的距离 struct

我开始被R打湿了脚,我对时间序列的概念是全新的。谁能告诉我正确的方向来计算每月%的变化

我有不同年份、不同月份、不同城镇的数据&价格和变化率如是

在第二步中,我想在上表中取从9月开始到8月的所有可能对的平均比率->也就是说,9比10,9比11,…,9比8,10比11,…,10比8。。。7_8

计算:

变化率函数:a-b/b*100,其中a表示新月份,b表示上月

平均_比率:所有年份和城镇中各月份的平均值

%变化:log1+平均/x*100, 其中x是开始月份和结束月份之间的距离

structure(list(hrvyear = c(1270, 1270, 1272, 1272, 1275, 1275
), m = c(5, 12, 2, 4, 2, 3), town = c("Chesterford", "Chesterford", 
"Lopham", "Lopham", "Lopham", "Lopham"), `mean(price)` = c(80, 
64, 74, 78, 59, 61)), row.names = c(NA, -6L), groups = structure(list(
    hrvyear = c(1270, 1270, 1272, 1272, 1275, 1275), m = c(5, 
    12, 2, 4, 2, 3), .rows = structure(list(1L, 2L, 3L, 4L, 5L, 
        6L), ptype = integer(0), class = c("vctrs_list_of", "vctrs_vctr", 
    "list"))), row.names = c(NA, 6L), class = c("tbl_df", "tbl", 
"data.frame"), .drop = TRUE), class = c("grouped_df", "tbl_df", 
"tbl", "data.frame"))

我希望问题是清楚的。非常感谢您的建议。

到目前为止,我在第一步中使用了此代码。但是,很明显,我不喜欢在每个月组重复多次该函数

may_july <- complete_mc %>%
  filter(
    m %in% c(5,7)
             ) %>%
  arrange(town, hrvyear, m)

# create new column, to check whether the previous month is from the same year and the same town (e.g. we start with may to july comparison)
roc <- c()
for (i in 1:nrow(may_july)) {
  if(may_july$hrvyear[i+1] == may_july$hrvyear[i] & may_july$town[i+1] == may_july$town[i]) {
    roc <- c(roc, TRUE)
  } else {
    roc <- c(roc, FALSE)
  }
}

# add FALSE for the first row of the roc column, as no previous row exists, 
# and in order to combine matrix with vector
roc <- c(FALSE, roc)
tm <- cbind(may_july, roc)

# if previous month is from the same year and the same town, calculate the ratio,
# if not, add NA
roc2 <- c()
for(i in 1:nrow(may_july)) {
  if(roc[i]==TRUE) {
    roc2 <- c(roc2, (may_july$mean_price[i+1] - may_july$mean_price[i]) / (may_july$mean_price[i]))
  } else {
    roc2 <- c(roc2, NA)
  }
}

# combine matrix with the final ratios
tt <- cbind(may_july, roc2)
roc3 <- na.omit(roc2)

# calculate the rate of change with the average ratio
may_to_july <- (log(1+mean(roc3))/2)*100
mean(roc3)
´´´

到目前为止,我在第一步中使用了这段代码。但是,很明显,我不喜欢在每个月组重复多次该函数

may_july <- complete_mc %>%
  filter(
    m %in% c(5,7)
             ) %>%
  arrange(town, hrvyear, m)

# create new column, to check whether the previous month is from the same year and the same town (e.g. we start with may to july comparison)
roc <- c()
for (i in 1:nrow(may_july)) {
  if(may_july$hrvyear[i+1] == may_july$hrvyear[i] & may_july$town[i+1] == may_july$town[i]) {
    roc <- c(roc, TRUE)
  } else {
    roc <- c(roc, FALSE)
  }
}

# add FALSE for the first row of the roc column, as no previous row exists, 
# and in order to combine matrix with vector
roc <- c(FALSE, roc)
tm <- cbind(may_july, roc)

# if previous month is from the same year and the same town, calculate the ratio,
# if not, add NA
roc2 <- c()
for(i in 1:nrow(may_july)) {
  if(roc[i]==TRUE) {
    roc2 <- c(roc2, (may_july$mean_price[i+1] - may_july$mean_price[i]) / (may_july$mean_price[i]))
  } else {
    roc2 <- c(roc2, NA)
  }
}

# combine matrix with the final ratios
tt <- cbind(may_july, roc2)
roc3 <- na.omit(roc2)

# calculate the rate of change with the average ratio
may_to_july <- (log(1+mean(roc3))/2)*100
mean(roc3)
´´´

您为此编写的函数几乎可以工作,但不要忘记将am$`meanprice`[i]-am$`meanprice`[i-1]放在括号中,这样在进行减法之前就不会进行除法

一个更简单的答案是在data.tables中使用shift函数,它类似于dplyr中的超前滞后函数。它们根据传递的参数选择前后行

library(data.table)
dt <- as.data.table(structure(list(hrvyear = c(1270, 1270, 1272, 1272, 1275, 1275
), m = c(5, 12, 2, 4, 2, 3), town = c("Chesterford", "Chesterford", 
                                      "Lopham", "Lopham", "Lopham", "Lopham"), `mean(price)` = c(80, 
                                                                                                 64, 74, 78, 59, 61)), row.names = c(NA, -6L), groups = structure(list(
                                                                                                   hrvyear = c(1270, 1270, 1272, 1272, 1275, 1275), m = c(5, 
                                                                                                                                                          12, 2, 4, 2, 3), .rows = structure(list(1L, 2L, 3L, 4L, 5L, 
                                                                                                                                                                                                  6L), ptype = integer(0), class = c("vctrs_list_of", "vctrs_vctr", 
                                                                                                                                                                                                                                     "list"))), row.names = c(NA, 6L), class = c("tbl_df", "tbl", 
                                                                                                                                                                                                                                                                                 "data.frame"), .drop = TRUE), class = c("grouped_df", "tbl_df", 
                                                                                                                                                                                                                                                                                                                         "tbl", "data.frame")))
 
# this changes the name of your mean(price) 
colnames(dt)[4] <- 'price'

dt[, rate := (price - shift(price))/price * 100]

dt
   hrvyear  m        town price       rate
1:    1270  5 Chesterford    80         NA
2:    1270 12 Chesterford    64 -25.000000
3:    1272  2      Lopham    74  13.513514
4:    1272  4      Lopham    78   5.128205
5:    1275  2      Lopham    59 -32.203390
6:    1275  3      Lopham    61   3.278689

您为此编写的函数几乎可以工作,但不要忘记将am$`meanprice`[i]-am$`meanprice`[i-1]放在括号中,这样在进行减法之前就不会进行除法

一个更简单的答案是在data.tables中使用shift函数,它类似于dplyr中的超前滞后函数。它们根据传递的参数选择前后行

library(data.table)
dt <- as.data.table(structure(list(hrvyear = c(1270, 1270, 1272, 1272, 1275, 1275
), m = c(5, 12, 2, 4, 2, 3), town = c("Chesterford", "Chesterford", 
                                      "Lopham", "Lopham", "Lopham", "Lopham"), `mean(price)` = c(80, 
                                                                                                 64, 74, 78, 59, 61)), row.names = c(NA, -6L), groups = structure(list(
                                                                                                   hrvyear = c(1270, 1270, 1272, 1272, 1275, 1275), m = c(5, 
                                                                                                                                                          12, 2, 4, 2, 3), .rows = structure(list(1L, 2L, 3L, 4L, 5L, 
                                                                                                                                                                                                  6L), ptype = integer(0), class = c("vctrs_list_of", "vctrs_vctr", 
                                                                                                                                                                                                                                     "list"))), row.names = c(NA, 6L), class = c("tbl_df", "tbl", 
                                                                                                                                                                                                                                                                                 "data.frame"), .drop = TRUE), class = c("grouped_df", "tbl_df", 
                                                                                                                                                                                                                                                                                                                         "tbl", "data.frame")))
 
# this changes the name of your mean(price) 
colnames(dt)[4] <- 'price'

dt[, rate := (price - shift(price))/price * 100]

dt
   hrvyear  m        town price       rate
1:    1270  5 Chesterford    80         NA
2:    1270 12 Chesterford    64 -25.000000
3:    1272  2      Lopham    74  13.513514
4:    1272  4      Lopham    78   5.128205
5:    1275  2      Lopham    59 -32.203390
6:    1275  3      Lopham    61   3.278689