r - 根据几个条件重塑数据框
问题描述
我想确定在时间 t 期间在同一地点和同一个人进行的活动。该变量wher
表示时间步长和活动在时间 t 发生的记录。with 参数记录在时间 t 与谁一起执行活动。我想知道在时间 t 基于性别在同一地点和同一个人进行的常见活动。不常见的活动和在不同地方与不同人进行的活动我用 0 代替。
输入
id DMSex t1 t2 t3 t4 wher1 wher2 wher3 wher4 wit1 wit2 wit3 wit4
12 M 12 12 12 12 1 1 1 4 8 9 4 0
12 F 10 13 12 12 3 1 1 5 6 5 4 1
输出:
id t1 t2 t3 t4
12 0 0 12 0
样本数据18 time steps
:
structure(list(serial = c(11011202, 11011202), DMSex = c(1, 2
), act1_1 = c(110, 110), act1_2 = c(110, 110), act1_3 = c(110,
110), act1_4 = c(110, 110), act1_5 = c(110, 110), act1_6 = c(110,
110), act1_7 = c(110, 110), act1_8 = c(110, 110), act1_9 = c(110,
110), act1_10 = c(110, 110), act1_11 = c(110, 110), act1_12 = c(8219,
110), act1_13 = c(310, 110), act1_14 = c(3210, 110), act1_15 = c(3110,
110), act1_16 = c(7241, 110), act1_17 = c(210, 110), act1_18 = c(3819,
110), wher_1 = c(11, 11), wher_2 = c(11, 11), wher_3 = c(11,
11), wher_4 = c(11, 11), wher_5 = c(11, 11), wher_6 = c(11, 11
), wher_7 = c(11, 11), wher_8 = c(11, 11), wher_9 = c(11, 11),
wher_10 = c(11, 11), wher_11 = c(11, 11), wher_12 = c(11,
11), wher_13 = c(11, 11), wher_14 = c(11, 11), wher_15 = c(11,
11), wher_16 = c(11, 11), wher_17 = c(11, 11), wher_18 = c(11,
11), wit4_1 = c(0, 0), wit4_2 = c(0, 0), wit4_3 = c(0, 0),
wit4_4 = c(0, 0), wit4_5 = c(0, 0), wit4_6 = c(0, 0), wit4_7 = c(0,
0), wit4_8 = c(0, 0), wit4_9 = c(0, 0), wit4_10 = c(0, 0),
wit4_11 = c(0, 0), wit4_12 = c(0, 0), wit4_13 = c(0, 0),
wit4_14 = c(0, 0), wit4_15 = c(0, 0), wit4_16 = c(0, 0),
wit4_17 = c(0, 0), wit4_18 = c(0, 0)), row.names = 1:2, class = "data.frame")
act1_
在哪里t
; wit4
是wit
和wher_
是wher
解决方案
一种解决方案结合dplyr
和purrr
可能是:
map(.x = as.character(1:4),
~ df %>%
select(id, ends_with(.x)) %>%
group_by(id) %>%
mutate_at(vars(matches("^wher|^wit")), ~ all(. == first(.))) %>%
ungroup() %>%
mutate(cond = rowSums(select(., matches("^wher|^wit"))) == 2) %>%
group_by(id) %>%
mutate_at(vars(starts_with("t")), ~ all(. == first(.)) * cond * .) %>%
ungroup() %>%
select(starts_with("t"))) %>%
bind_cols(df %>%
select(id)) %>%
group_by(id) %>%
summarise_all(first)
id t1 t2 t3 t4
<int> <int> <int> <int> <int>
1 12 0 0 12 0
首先,它创建一个从 1 到 4 的字符向量,因为有四对变量(从 t1、wher1、wit1 到 t4、wher4、wit4)。映射函数应用于这些元素。其次,它从 df 中单独选择变量对并检查每个 ID 的所有行中 wher 和 wit 是否相同,从而创建一个逻辑条件。第三,它检查每个 ID 的所有行中的 t 变量是否相同,并将其与步骤 2 中的逻辑条件进行比较。如果为 TRUE,则返回原始值,否则返回 0。最后,它结合数据并保持每个 ID 一行。
更新问题的解决方案,添加stringr
:
map(.x = str_extract(names(df)[grepl("^act", names(df))], "_.*+$"),
~ df %>%
select(serial, ends_with(.x)) %>%
group_by(serial) %>%
mutate_at(vars(matches("^wher|^wit")), ~ all(. == first(.))) %>%
ungroup() %>%
mutate(cond = rowSums(select(., matches("^wher|^wit"))) == 2) %>%
group_by(serial) %>%
mutate_at(vars(starts_with("act")), ~ all(. == first(.)) * cond * .) %>%
ungroup() %>%
select(starts_with("act"))) %>%
bind_cols(df %>%
select(serial)) %>%
group_by(serial) %>%
summarise_all(first)
serial act1_1 act1_2 act1_3 act1_4 act1_5 act1_6 act1_7 act1_8 act1_9 act1_10 act1_11 act1_12
<dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 1.10e7 110 110 110 110 110 110 110 110 110 110 110 0
# … with 6 more variables: act1_13 <dbl>, act1_14 <dbl>, act1_15 <dbl>, act1_16 <dbl>,
# act1_17 <dbl>, act1_18 <dbl>
推荐阅读
- docker - 每个用户会话的一次性 docker 实例
- python - 使用装饰器(python)预处理类调用
- python - 如何处理具有多个序列的时间序列预测分析
- google-apps-script - google.script.run 函数不会生成 Logger.log
- r - 模拟来自三态 HMM 模型的数据
- react-native - 路线导航到其他地方
- typescript - Typescript-“类型响应中不存在属性 ip”
- python - 如何将值列表从字典转换为字符串?
- python - 获取括号中的单词作为组正则表达式
- compiler-construction - x84 代码与 Arm 代码有何不同。你不需要一个新的编译器吗?