r - Using a value of column to get an element of a separate list (in a mutate() call)
问题描述
I am trying to use values from two columns in a row to essentially do a lookup within a set in a separate list.
A code snippet is below:
library(tidyverse)
# Sample days and hour of day
df_hours <- tibble(
dow = as.integer(c(rep(1, 5), rep(2, 5))),
hour = as.integer(rep(c(0:4), 2)),
)
# Sample hours open. One set per day of week
open_hrs <- list(c(01:03), c(03:04))
# Show before
df_hours
The data:
# A tibble: 10 x 2
dow hour
<int> <int>
1 1 0
2 1 1
3 1 2
4 1 3
5 1 4
6 2 0
7 2 1
8 2 2
9 2 3
10 2 4
Then attempting the lookup:
# Lookup the hour in the dow open_hrs set and add the result for the row
df_hours <- df_hours %>%
mutate(
open = ifelse(hour %in% unlist(open_hrs[ dow ]), 1, 0)
)
# Show result
df_hours
The result:
# A tibble: 10 x 3
dow hour open
<int> <int> <dbl>
1 1 0 0
2 1 1 1
3 1 2 1
4 1 3 1
5 1 4 1
6 2 0 0
7 2 1 1
8 2 2 1
9 2 3 1
10 2 4 1
This seems to have used a combination of all values in "open_hrs". I believe the issues is that "dow" is not being passed is as the value for that "row".
What I'm seeking as a result is:
dow hour open
<int> <int> <dbl>
1 1 0 0
2 1 1 1
3 1 2 1
4 1 3 1
5 1 4 0
6 2 0 0
7 2 1 0
8 2 2 0
9 2 3 1
10 2 4 1
I've also tried to use sapply() also but without success.
Thanks in advance.
解决方案
当您尝试检查是否hour
存在于您的单独列表中时:
hour %in% unlist(open_hrs[ dow ])
dow
被视为 的整个列df_hours
。(跑去unlist(open_hrs[df_hours$dow])
看看我的意思)。您可以通过逐行执行此操作来实现您的目标,但更好的是对列表group_by
中的每个元素进行检查open_hrs
,如下所示:
df_hours %>%
group_by(dow) %>%
mutate(open = ifelse(hour %in% unlist(open_hrs[dow]), 1, 0))
# A tibble: 10 x 3
# Groups: dow [2]
dow hour open
<int> <int> <dbl>
1 0 0
1 1 1
1 2 1
1 3 1
1 4 0
2 0 0
2 1 0
2 2 0
2 3 1
2 4 1
(如果您有不期望分组的后续操作,请不要忘记取消分组!)
推荐阅读
- android - 如何在videoview中打开相机
- .net-core - 有没有办法引用一个在发布管道下批准了特定阶段的 nuget 包?
- python - 打开显式保存为 UTF-8 的 CSV 仍显示其编码为 cp1252
- git - git pull 真的是 git fetch + git merge 吗?
- python - 在包中找不到方法
- sql-server - 如何防止用户在代理服务帐户的上下文中运行作业?
- asp.net - 从声明中读取值
- mongodb - 如何按小时聚合 MongoDB 文档?
- reactjs - 用去抖动反应受控输入
- umbraco - 我在 Umbraco version8 的设置页面中找不到布局文件,但它可以在网站上正常显示