在“地图循环”中,更改行值如何在R-Studio?

发布于 2025-01-23 13:51:44 字数 868 浏览 3 评论 0原文

我有此数据框:

df <- structure(list(a = c(2, 5, 90, 77, 56, 65, 85, 75, 12, 24, 52, 
32), b = c(45, 78, 98, 55, 63, 12, 23, 38, 75, 68, 99, 73), c = c(77, 
85, 3, 22, 4, 69, 86, 39, 78, 36, 96, 11), d = c(52, 68, 4, 25, 
79, 120, 97, 20, 7, 19, 37, 67), e = c(14, 73, 91, 87, 94, 38, 
1, 685, 47, 102, 666, 74)), class = "data.frame", row.names = c(NA, 
-12L))

和脚本:

R <- Map(`+`, list(1:3), 0:3)
df_cum <- as.matrix(rep(NA, ncol(df)))
for (r in seq(R)) {
  for (f in seq(ncol(df))) {
    df_cum <- sapply(df[R[[r]],], function(x) (cumprod(1 + x) - 1)*100)
  }
}

我想将所有第一行值更改为“ 0”,对于每个循环(1:3,2:4,3:5,...),然后在

df_cum <- sapply(df[R[[r]],], function(x) (cumprod(1 + x) - 1)*100)

I.E. 之前对于第一个cicle 1:3(df行),第一行值从“ 2、45、77、52、14”变为“ 0、0、0、0、0、0”。

我该怎么办? 谢谢

I have this dataframe:

df <- structure(list(a = c(2, 5, 90, 77, 56, 65, 85, 75, 12, 24, 52, 
32), b = c(45, 78, 98, 55, 63, 12, 23, 38, 75, 68, 99, 73), c = c(77, 
85, 3, 22, 4, 69, 86, 39, 78, 36, 96, 11), d = c(52, 68, 4, 25, 
79, 120, 97, 20, 7, 19, 37, 67), e = c(14, 73, 91, 87, 94, 38, 
1, 685, 47, 102, 666, 74)), class = "data.frame", row.names = c(NA, 
-12L))

and the script:

R <- Map(`+`, list(1:3), 0:3)
df_cum <- as.matrix(rep(NA, ncol(df)))
for (r in seq(R)) {
  for (f in seq(ncol(df))) {
    df_cum <- sapply(df[R[[r]],], function(x) (cumprod(1 + x) - 1)*100)
  }
}

I want to change all the first row values to "0", for each loop (1:3, 2:4, 3:5,...), before

df_cum <- sapply(df[R[[r]],], function(x) (cumprod(1 + x) - 1)*100)

I.e. for the first cicle 1:3 (df rows), the first row values change from "2, 45, 77, 52, 14" to "0, 0, 0, 0, 0".

How can I do?
Thx

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

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

发布评论

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