Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/svg/2.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
如何使用for循环创建和填充列?_R_For Loop_Data.table - Fatal编程技术网

如何使用for循环创建和填充列?

如何使用for循环创建和填充列?,r,for-loop,data.table,R,For Loop,Data.table,我有一个包含10个变量的简单时间序列数据集-我想创建一个for循环(或函数),为时间序列中的每个变量(日期除外)创建一个“上个月的变化”变量和一个“上个月的变化百分比”。我知道我可以简单地为每个特定的列编写代码,但我想优化它,因为有很多列 以下是我的数据,“日期”、“销售额”、“价格”是一些列名: +----+---+---+---+---+---+---+---+-- | Date | Sales | Price | +----+---+---+---+---+---

我有一个包含10个变量的简单时间序列数据集-我想创建一个for循环(或函数),为时间序列中的每个变量(日期除外)创建一个“上个月的变化”变量和一个“上个月的变化百分比”。我知道我可以简单地为每个特定的列编写代码,但我想优化它,因为有很多列

以下是我的数据,“日期”、“销售额”、“价格”是一些列名:

+----+---+---+---+---+---+---+---+--
| Date       |   Sales   |  Price  | 
+----+---+---+---+---+---+---+---+--
| 01Aug2019  | 4         | 15      |
| 01Sept2019 | 6         | 30      |
| 01Oct2019  | 10        | 44      |
+----+---+---+---+---+---+---+---+--
下面是我希望它在使用for循环(或任何函数)时的外观

我尝试了下面的代码,但没有成功

add_column <- function (x, y){
  setDT (x)[,pct_chg_y:= (y - shift (y,1, type="lag")/shift (,1, type="lag")*100]

}

add\u column这里有一个带有
data.table
的选项,我们在
.SDcols
中指定感兴趣的列,通过从
.SD
滞后值中减去
.SD
(data.table的子集)来创建“chg”列,即
.SD
shift
,然后在第二步,创建“pct\chg”,使用
Map

nm1 <- c("Sales", "Price")
setDT(df1)[,  paste0("chg_", nm1)  :=  .SD - shift(.SD), .SDcols = nm1]
df1[, paste0("pct_chg_", nm1) :=   
      Map(function(x, y)  100 * (y/shift(x)), .SD, mget(paste0("chg_", nm1))),
               .SDcols = nm1]
df1
#         Date Sales Price chg_Sales chg_Price pct_chg_Sales pct_chg_Price
#1:  01Aug2019     4    15        NA        NA            NA            NA
#2: 01Sept2019     6    30         2        15      50.00000     100.00000
#3:  01Oct2019    10    44         4        14      66.66667      46.66667
nm1
nm1 <- c("Sales", "Price")
setDT(df1)[,  paste0("chg_", nm1)  :=  .SD - shift(.SD), .SDcols = nm1]
df1[, paste0("pct_chg_", nm1) :=   
      Map(function(x, y)  100 * (y/shift(x)), .SD, mget(paste0("chg_", nm1))),
               .SDcols = nm1]
df1
#         Date Sales Price chg_Sales chg_Price pct_chg_Sales pct_chg_Price
#1:  01Aug2019     4    15        NA        NA            NA            NA
#2: 01Sept2019     6    30         2        15      50.00000     100.00000
#3:  01Oct2019    10    44         4        14      66.66667      46.66667
df1 <- structure(list(Date = c("01Aug2019", "01Sept2019", "01Oct2019"
), Sales = c(4, 6, 10), Price = c(15, 30, 44)), 
        class = "data.frame", row.names = c(NA, 
-3L))
library(dplyr)
library(scales)

df1 %>% 
  arrange(Date) %>% 
  mutate_at(.vars = c("Sales", "Price"), list(chg = ~(. - lag(.)),
                                              pct_chg = ~percent((. - lag(.))/lag(.))))

  #         Date Sales Price Sales_chg Price_chg Sales_pct_chg Price_pct_chg
  # 1 2019-08-01     4    15        NA        NA           NA%           NA%
  # 2 2019-09-01     6    30         2        15         50.0%        100.0%
  # 3 2019-10-01    10    44         4        14         66.7%         46.7%