R:每年获取一列以获取面板数据

发布于 2025-01-22 12:32:27 字数 1584 浏览 0 评论 0原文

我想赢得“返回”列。但是,我想每年赢得胜利。 我的数据看起来像这样:

structure(list(Name = c("A", "A", "A", "A", "A", "A", "B", "B", 
"B", "B", "B", "B", "C", "C", "C", "C", "C", "C"), Date = c("01.09.2018", 
"02.09.2018", "03.09.2018", "05.11.2021", "06.11.2021", "07.11.2021", 
"01.09.2018", "02.09.2018", "03.09.2018", "05.11.2021", "06.11.2021", 
"07.11.2021", "01.09.2018", "02.09.2018", "03.09.2018", "05.11.2021", 
"06.11.2021", "07.11.2021"), Return = c(0.05, 0.1, 0.8, 1.5, 
0.1, -2, 0.4, 0.6, 0.6, 0.2, -0.5, -0.9, -0.4, 1.7, 0.3, -4, 
0.6, 0.5), Year = c("2018", "2018", "2018", "2021", "2021", "2021", 
"2018", "2018", "2018", "2021", "2021", "2021", "2018", "2018", 
"2018", "2021", "2021", "2021")), row.names = c(NA, -18L), class = c("grouped_df", 
"tbl_df", "tbl", "data.frame"), groups = structure(list(Year = c("2018", 
"2021"), .rows = structure(list(c(1L, 2L, 3L, 7L, 8L, 9L, 13L, 
14L, 15L), c(4L, 5L, 6L, 10L, 11L, 12L, 16L, 17L, 18L)), ptype = integer(0), class = c("vctrs_list_of", 
"vctrs_vctr", "list"))), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -2L), .drop = TRUE))

我尝试了以下代码也有效:

Data <- Data %>%
  group_by(Year) %>%
  mutate("Winsorized Return" = Winsorize(`Return`, probs=c(0.01, 0.99)))

现在我执行了相同的操作,但每年没有对数据进行分组:

Data <- Data %>%
  mutate("Winsorized Return" = Winsorize(`Return`, probs=c(0.01, 0.99)))

即使我在第一个代码中使用了“ group_by(年),我也得到了完全相同的结果。 有人可以向我解释为什么结果是一样的吗?即使我对我的真实(非常大)数据集进行了同样的事情,也会得到相同的结果。
我是否必须使用另一个代码才能每年获取“返回”列?

非常感谢您的任何帮助!

I would like to winsorize the column "Return". However, I want to winsorize per Year.
My data looks like this:

structure(list(Name = c("A", "A", "A", "A", "A", "A", "B", "B", 
"B", "B", "B", "B", "C", "C", "C", "C", "C", "C"), Date = c("01.09.2018", 
"02.09.2018", "03.09.2018", "05.11.2021", "06.11.2021", "07.11.2021", 
"01.09.2018", "02.09.2018", "03.09.2018", "05.11.2021", "06.11.2021", 
"07.11.2021", "01.09.2018", "02.09.2018", "03.09.2018", "05.11.2021", 
"06.11.2021", "07.11.2021"), Return = c(0.05, 0.1, 0.8, 1.5, 
0.1, -2, 0.4, 0.6, 0.6, 0.2, -0.5, -0.9, -0.4, 1.7, 0.3, -4, 
0.6, 0.5), Year = c("2018", "2018", "2018", "2021", "2021", "2021", 
"2018", "2018", "2018", "2021", "2021", "2021", "2018", "2018", 
"2018", "2021", "2021", "2021")), row.names = c(NA, -18L), class = c("grouped_df", 
"tbl_df", "tbl", "data.frame"), groups = structure(list(Year = c("2018", 
"2021"), .rows = structure(list(c(1L, 2L, 3L, 7L, 8L, 9L, 13L, 
14L, 15L), c(4L, 5L, 6L, 10L, 11L, 12L, 16L, 17L, 18L)), ptype = integer(0), class = c("vctrs_list_of", 
"vctrs_vctr", "list"))), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -2L), .drop = TRUE))

I tried the following code which also works:

Data <- Data %>%
  group_by(Year) %>%
  mutate("Winsorized Return" = Winsorize(`Return`, probs=c(0.01, 0.99)))

Now I do the same but without grouping the data per year:

Data <- Data %>%
  mutate("Winsorized Return" = Winsorize(`Return`, probs=c(0.01, 0.99)))

I get exactly the same results even though I used "group_by(Years) in the first code.
Can someone explain me why the results are the same? Even when I do the same with my real (very large) dataset I get the same results.
Do I have to use another code in order to winsorize the column "Return" per year?

Thank you very much already for any help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文