我有一个基于GPS数据的数据框,我想创建多个长度的滚动总和变量。我可以使用许多dplyr::mutate
library(dplyr)
library(zoo) # rollsumr
df_1 %<>%
mutate(1min = rollsumr(Dist, 600, fill = NA),
2min = rollsumr(Dist, 1200, fill = NA),
3min = rollsumr(Dist, 1800, fill = NA),
4min = rollsumr(Dist, 2400, fill = NA),
5min = rollsumr(Dist, 3000, fill = NA),
6min = rollsumr(Dist, 3600, fill = NA),
7min = rollsumr(Dist, 4200, fill = NA),
8min = rollsumr(Dist, 4800, fill = NA),
9min = rollsumr(Dist, 5400, fill = NA),
10min = rollsumr(Dist, 6000, fill = NA))
但是我想知道是否存在一种使用mutate_at
同时创建它们的方法?我尝试过自己,但无法管理。
在使用非常大的数据集处理数据表时,我确实有一个单独的脚本,但是对于较小的数据集,dplyr
和mutate
更快。
setDT(df_1)[paste0(1:15) := lapply((1:15)*600, function(x) rollsumr(Dist, x, fill = NA))][]
样本数据
> dput(head(df_1,40))
structure(list(Seconds = c(0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7,
0.8, 0.9, 1, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8, 1.9, 2,
2.1, 2.2, 2.3, 2.4, 2.5, 2.6, 2.7, 2.8, 2.9, 3, 3.1, 3.2, 3.3,
3.4, 3.5, 3.6, 3.7, 3.8, 3.9), Dist = c(0, 0.122, 0.116, 0.122,
0.127, 0.133, 0.144, 0.143, 0.143, 0.133, 0.13, 0.133, 0.139,
0.146, 0.155, 0.164, 0.159, 0.16, 0.155, 0.156, 0.155, 0.158,
0.146, 0.148999999999999, 0.154, 0.159, 0.158, 0.166, 0.168999999999999,
0.156, 0.151, 0.145, 0.152, 0.163999999999999, 0.169, 0.17, 0.169,
0.177, 0.177999999999999, 0.175)), row.names = c(NA, 40L), class = "data.frame")
PS-单个数据帧将包含约70,000行数据,我不确定如何给出可复制的样本?
答案 0 :(得分:3)
使用purrr::map_dfc
这样的事情怎么样?
windows <- setNames(1:10, paste0("min", 1:10))
df_1 %>%
bind_cols(
map_dfc(windows, ~rollsumr(df_1$Dist, .x, fill = NA)) %>%
setNames(., names(windows)))
# Seconds Dist min1 min2 min3 min4 min5 min6 min7 min8 min9 min10
#1 0.0 0.000 0.000 NA NA NA NA NA NA NA NA NA
#2 0.1 0.122 0.122 0.122 NA NA NA NA NA NA NA NA
#3 0.2 0.116 0.116 0.238 0.238 NA NA NA NA NA NA NA
#4 0.3 0.122 0.122 0.238 0.360 0.360 NA NA NA NA NA NA
#5 0.4 0.127 0.127 0.249 0.365 0.487 0.487 NA NA NA NA NA
#6 0.5 0.133 0.133 0.260 0.382 0.498 0.620 0.620 NA NA NA NA
#7 0.6 0.144 0.144 0.277 0.404 0.526 0.642 0.764 0.764 NA NA NA
#8 0.7 0.143 0.143 0.287 0.420 0.547 0.669 0.785 0.907 0.907 NA NA
#9 0.8 0.143 0.143 0.286 0.430 0.563 0.690 0.812 0.928 1.050 1.050 NA
#10 0.9 0.133 0.133 0.276 0.419 0.563 0.696 0.823 0.945 1.061 1.183 1.183
#11 1.0 0.130 0.130 0.263 0.406 0.549 0.693 0.826 0.953 1.075 1.191 1.313
#12 1.1 0.133 0.133 0.263 0.396 0.539 0.682 0.826 0.959 1.086 1.208 1.324
#13 1.2 0.139 0.139 0.272 0.402 0.535 0.678 0.821 0.965 1.098 1.225 1.347
#14 1.3 0.146 0.146 0.285 0.418 0.548 0.681 0.824 0.967 1.111 1.244 1.371
#15 1.4 0.155 0.155 0.301 0.440 0.573 0.703 0.836 0.979 1.122 1.266 1.399
#16 1.5 0.164 0.164 0.319 0.465 0.604 0.737 0.867 1.000 1.143 1.286 1.430
#17 1.6 0.159 0.159 0.323 0.478 0.624 0.763 0.896 1.026 1.159 1.302 1.445
#18 1.7 0.160 0.160 0.319 0.483 0.638 0.784 0.923 1.056 1.186 1.319 1.462
#19 1.8 0.155 0.155 0.315 0.474 0.638 0.793 0.939 1.078 1.211 1.341 1.474
#20 1.9 0.156 0.156 0.311 0.471 0.630 0.794 0.949 1.095 1.234 1.367 1.497
#21 2.0 0.155 0.155 0.311 0.466 0.626 0.785 0.949 1.104 1.250 1.389 1.522
#22 2.1 0.158 0.158 0.313 0.469 0.624 0.784 0.943 1.107 1.262 1.408 1.547
#23 2.2 0.146 0.146 0.304 0.459 0.615 0.770 0.930 1.089 1.253 1.408 1.554
#24 2.3 0.149 0.149 0.295 0.453 0.608 0.764 0.919 1.079 1.238 1.402 1.557
#25 2.4 0.154 0.154 0.303 0.449 0.607 0.762 0.918 1.073 1.233 1.392 1.556
#26 2.5 0.159 0.159 0.313 0.462 0.608 0.766 0.921 1.077 1.232 1.392 1.551
#27 2.6 0.158 0.158 0.317 0.471 0.620 0.766 0.924 1.079 1.235 1.390 1.550
#28 2.7 0.166 0.166 0.324 0.483 0.637 0.786 0.932 1.090 1.245 1.401 1.556
#29 2.8 0.169 0.169 0.335 0.493 0.652 0.806 0.955 1.101 1.259 1.414 1.570
#30 2.9 0.156 0.156 0.325 0.491 0.649 0.808 0.962 1.111 1.257 1.415 1.570
#31 3.0 0.151 0.151 0.307 0.476 0.642 0.800 0.959 1.113 1.262 1.408 1.566
#32 3.1 0.145 0.145 0.296 0.452 0.621 0.787 0.945 1.104 1.258 1.407 1.553
#33 3.2 0.152 0.152 0.297 0.448 0.604 0.773 0.939 1.097 1.256 1.410 1.559
#34 3.3 0.164 0.164 0.316 0.461 0.612 0.768 0.937 1.103 1.261 1.420 1.574
#35 3.4 0.169 0.169 0.333 0.485 0.630 0.781 0.937 1.106 1.272 1.430 1.589
#36 3.5 0.170 0.170 0.339 0.503 0.655 0.800 0.951 1.107 1.276 1.442 1.600
#37 3.6 0.169 0.169 0.339 0.508 0.672 0.824 0.969 1.120 1.276 1.445 1.611
#38 3.7 0.177 0.177 0.346 0.516 0.685 0.849 1.001 1.146 1.297 1.453 1.622
#39 3.8 0.178 0.178 0.355 0.524 0.694 0.863 1.027 1.179 1.324 1.475 1.631
#40 3.9 0.175 0.175 0.353 0.530 0.699 0.869 1.038 1.202 1.354 1.499 1.650
请注意,我更改了窗口,以为您提供的示例数据提供合理的非NA
值。