首页 > 解决方案 > 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.

标签: rdplyr

解决方案


当您尝试检查是否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

(如果您有不期望分组的后续操作,请不要忘记取消分组!)


推荐阅读