r - 在R中的数据框中移动值
问题描述
我有一个数据框:
dt <- read.table(text = "
0 344 34 0 0
0 350 16 0 0
0 366 11 0 0
0 376 8 0 0
0 380 28 0 0
0 397 55 0 0
0 398 45 0 0
0 400 19 0 0
0 402 30 0 0")
我想将 1/3 的行随机向右移动一列,以获得例如:
dt1 <- read.table(text = "
0 344 34 0 0
0 350 16 0 0
0 0 366 11 0
0 376 8 0 0
0 0 380 28 0
0 0 397 55 0
0 398 45 0 0
0 400 19 0 0
0 402 30 0 0")
是否可以有一个参数来选择班次数?如果我想移动两列、三列或四列而不是一列。
解决方案
以下是两种可能的解决方案,如果您愿意,可以将其封装为一个函数。
第一个解决方案的理念是转置您的数据集并将行用作列,以便您可以使用mutate_at
(您随机选择的行)然后再次转置:
dt <- read.table(text = "
0 344 34 0 0
0 350 16 0 0
0 366 11 0 0
0 376 8 0 0
0 380 28 0 0
0 397 55 0 0
0 398 45 0 0
0 400 19 0 0
0 402 30 0 0")
library(tidyverse)
# for reproducibility
set.seed(4)
# pick number of rows to shift
num_rows = round(1/3 * nrow(dt))
# sample position of those rows
rows = sample(1:nrow(dt), num_rows)
# specify number of shifts
num_shifts = 2
t(dt) %>% # transpose dataset
data.frame() %>% # update to data frame
mutate_at(rows, ~lag(., num_shifts, default = 0L)) %>% # use the selected row positions and the selected number of shifts to apply this function
t() %>% # transapose data again
data.frame(., row.names = NULL) # update to dataframe
# X1 X2 X3 X4 X5
# 1 0 0 0 344 34
# 2 0 350 16 0 0
# 3 0 0 0 366 11
# 4 0 376 8 0 0
# 5 0 380 28 0 0
# 6 0 0 0 397 55
# 7 0 398 45 0 0
# 8 0 400 19 0 0
# 9 0 402 30 0 0
还有一种替代方法,无需转置,而是使用以下方式处理行map2
:
dt %>%
group_by(id = row_number()) %>% # group by row id
nest() %>% # nest data
mutate(d = map2(id, data, ~if(.x %in% rows) lag(.y, num_shifts, default = 0L) else .y)) %>% # apply shift to selected row positions/ids
unnest(d) %>% # unnest data
select(-id, -data) # remove unnecessary columns
# # A tibble: 9 x 5
# V1 V2 V3 V4 V5
# <int> <int> <int> <int> <int>
# 1 0 0 0 344 34
# 2 0 350 16 0 0
# 3 0 0 0 366 11
# 4 0 376 8 0 0
# 5 0 380 28 0 0
# 6 0 0 0 397 55
# 7 0 398 45 0 0
# 8 0 400 19 0 0
# 9 0 402 30 0 0
推荐阅读
- visual-studio - 为什么我们不能在非系统驱动器上安装大部分 Visual Studio 2019?
- sql - 如何从提到的查询中获取需要记录
- css - 替换搜索和购物篮图标 shopisle
- python - pyspark 中的分桶和一种热编码
- c# - C# WPF:模糊文本
- javascript - 如何在量角器中与黄瓜框架共享网络驱动程序
- python - 如何在 pyplot 中使用 xlim 绘制时间戳
- netlogo - 设置半径寻找伙伴龟
- markdown - 如何评论一个markdown标签
- c# - Async/await, Task Parallel 对于在调试时运行 FINE 但无法运行的复杂进程