添加多个按一年滞后的列

huangapple go评论70阅读模式
英文:

Add multiple columns lagged by one year

问题

我需要在我的数据框中添加多列的1年滞后版本。以下是我的数据:

```R
data<-data.frame(Year=c("2011","2011","2011","2012","2012","2012","2013","2013","2013"), 
                 Country=c("America","China","India","America","China","India","America","China","India"),
                 Value1=c(234,443,754,334,117,112,987,903,476),
                 Value2=c(2,4,5,6,7,8,1,2,2))

我想要添加两列,包含Value1和Value2在t-1时的值,以使我的数据框看起来像这样:

添加多个按一年滞后的列

我该如何做到这一点?这样做是否是通过年份滞后我的变量的正确方式?

提前感谢!


<details>
<summary>英文:</summary>

I need to add a 1-year-lagged version of multiple columns from my dataframe. Here&#39;s my data:

data<-data.frame(Year=c("2011","2011","2011","2012","2012","2012","2013","2013","2013"),
Country=c("America","China","India","America","China","India","America","China","India"),
Value1=c(234,443,754,334,117,112,987,903,476),
Value2=c(2,4,5,6,7,8,1,2,2))


And I want to add two columns that contain Value1 and Value2 at t-1, so that my dataframe looks like this:

[![enter image description here][1]][1]


  [1]: https://i.stack.imgur.com/EFrpk.png

How can I do this? Would this be the correct way to lag my variables by year?

Thanks in advance!

</details>


# 答案1
**得分**: 4

使用 *data.table*:

```R
library(data.table)

setDT(data)
cols <- grep("^Value", colnames(data), value = TRUE)
data[, paste0(cols, "_lag") := lapply(.SD, shift), .SDcols = cols, by = Country]
#    Year Country Value1 Value2 Value1_lag Value2_lag
# 1: 2011 America    234      2         NA         NA
# 2: 2011   China    443      4         NA         NA
# 3: 2011   India    754      5         NA         NA
# 4: 2012 America    334      6        234          2
# 5: 2012   China    117      7        443          4
# 6: 2012   India    112      8        754          5
# 7: 2013 America    987      1        334          6
# 8: 2013   China    903      2        117          7
# 9: 2013   India    476      2        112          8
英文:

Using data.table:

library(data.table)

setDT(data)
cols &lt;- grep(&quot;^Value&quot;, colnames(data), value = TRUE)
data[, paste0(cols, &quot;_lag&quot;) := lapply(.SD, shift), .SDcols = cols, by = Country]
#    Year Country Value1 Value2 Value1_lag Value2_lag
# 1: 2011 America    234      2         NA         NA
# 2: 2011   China    443      4         NA         NA
# 3: 2011   India    754      5         NA         NA
# 4: 2012 America    334      6        234          2
# 5: 2012   China    117      7        443          4
# 6: 2012   India    112      8        754          5
# 7: 2013 America    987      1        334          6
# 8: 2013   China    903      2        117          7
# 9: 2013   India    476      2        112          8

答案2

得分: 2

dplyr中,按组使用lag

library(dplyr) #1.1.0
data %>%
  mutate(across(contains("Value"), lag, .names = "{col}_lagged"), .by = Country)

  Year Country Value1 Value2 Value1_lagged Value2_lagged
1 2011 America 234 2 NA NA
2 2011 China 443 4 NA NA
3 2011 India 754 5 NA NA
4 2012 America 334 6 234 2
5 2012 China 117 7 443 4
6 2012 India 112 8 754 5
7 2013 America 987 1 334 6
8 2013 China 903 2 117 7
9 2013 India 476 2 112 8

在1.1.0以下版本:

data %>%
  group_by(Country) %>%
  mutate(across(c(GDP, Population), lag, .names = "{col}_lagged")) %>%
  ungroup()
英文:

In dplyr, use lag by group:

library(dplyr) #1.1.0
data %&gt;% 
  mutate(across(contains(&quot;Value&quot;), lag, .names = &quot;{col}_lagged&quot;), .by = Country)

  Year Country Value1 Value2 Value1_lagged Value2_lagged
1 2011 America    234      2            NA            NA
2 2011   China    443      4            NA            NA
3 2011   India    754      5            NA            NA
4 2012 America    334      6           234             2
5 2012   China    117      7           443             4
6 2012   India    112      8           754             5
7 2013 America    987      1           334             6
8 2013   China    903      2           117             7
9 2013   India    476      2           112             8

Below 1.1.0:

data %&gt;% 
  group_by(Country) %&gt;%
  mutate(across(c(GDP, Population), lag, .names = &quot;{col}_lagged&quot;)) %&gt;%
  ungroup()

</details>



# 答案3
**得分**: 0

以下是您提供的代码的中文翻译:

```R
另一种使用`dplyr`来完成任务的方法。

    library(dplyr)
    data_lagged <- data %>%
      group_by(Country) %>%
      mutate(Value1_Lagged = lag(Value1),
             Value2_Lagged = lag(Value2),
             Year = as.integer(as.character(Year)) + 1)
    data_final <- cbind(data, data_lagged[, c("Value1_Lagged", "Value2_Lagged")])
    data_final

输出结果:

  Year Country Value1 Value2 Value1_Lagged Value2_Lagged
1 2011 America    234      2            NA            NA
2 2011   China    443      4            NA            NA
3 2011   India    754      5            NA            NA
4 2012 America    334      6           234             2
5 2012   China    117      7           443             4
6 2012   India    112      8           754             5
7 2013 America    987      1           334             6
8 2013   China    903      2           117             7
9 2013   India    476      2           112             8

请注意,我已经将代码中的注释和代码部分翻译成中文。

<details>
<summary>英文:</summary>

Another way using `dplyr` to ge tthe job done.

    library(dplyr)
    data_lagged &lt;- data %&gt;%
      group_by(Country) %&gt;%
      mutate(Value1_Lagged = lag(Value1),
             Value2_Lagged = lag(Value2),
             Year = as.integer(as.character(Year)) + 1)
    data_final &lt;- cbind(data, data_lagged[, c(&quot;Value1_Lagged&quot;, &quot;Value2_Lagged&quot;)])
    data_final


Output:

      Year Country Value1 Value2 Value1_Lagged Value2_Lagged
    1 2011 America    234      2            NA            NA
    2 2011   China    443      4            NA            NA
    3 2011   India    754      5            NA            NA
    4 2012 America    334      6           234             2
    5 2012   China    117      7           443             4
    6 2012   India    112      8           754             5
    7 2013 America    987      1           334             6
    8 2013   China    903      2           117             7
    9 2013   India    476      2           112             8

</details>



huangapple
  • 本文由 发表于 2023年2月8日 18:03:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/75384121.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定