R 创建一个变量,该变量标记在另一列中找到的一列中的第一个观察值

R 创建一个变量,该变量标记在另一列中找到的一列中的第一个观察值,r,dplyr,tidyverse,R,Dplyr,Tidyverse,在数据中,关键变量是:“监听者”和“说话者”。为每个“线程”观察到的第一个侦听器是该线程的原始编写器 我正在尝试创建一个单独的变量“writer invention”,它在二进制0,1中标记线程的writer是演讲者的行 测试数据: 最终结果如下所示: ╔═══════╦════════╦══════════╦═════════╦════════════════════╦═══════════════════════════════════════════════════════════════

在数据中,关键变量是:“监听者”和“说话者”。为每个“线程”观察到的第一个侦听器是该线程的原始编写器

我正在尝试创建一个单独的变量“writer invention”,它在二进制0,1中标记线程的writer是演讲者的行

测试数据:

最终结果如下所示:

╔═══════╦════════╦══════════╦═════════╦════════════════════╦═══════════════════════════════════════════════════════════════╗
║ topic ║ thread ║ listener ║ speaker ║ writer_involvement ║ explanation                                                   ║
╠═══════╬════════╬══════════╬═════════╬════════════════════╬═══════════════════════════════════════════════════════════════╣
║   1   ║   10   ║    111   ║   222   ║          0         ║ The first observed listener (111) is the writer of the thread ║
╠═══════╬════════╬══════════╬═════════╬════════════════════╬═══════════════════════════════════════════════════════════════╣
║   1   ║   10   ║    111   ║   333   ║          0         ║                                                               ║
╠═══════╬════════╬══════════╬═════════╬════════════════════╬═══════════════════════════════════════════════════════════════╣
║   1   ║   10   ║    222   ║   111   ║          1         ║ The writer of this thread, 111, spoke here                    ║
╠═══════╬════════╬══════════╬═════════╬════════════════════╬═══════════════════════════════════════════════════════════════╣
║   1   ║   10   ║    111   ║   444   ║          0         ║                                                               ║
╠═══════╬════════╬══════════╬═════════╬════════════════════╬═══════════════════════════════════════════════════════════════╣
║   2   ║    3   ║    222   ║   444   ║          0         ║ The first observed listener (222) is the writer               ║
╠═══════╬════════╬══════════╬═════════╬════════════════════╬═══════════════════════════════════════════════════════════════╣
║   2   ║    3   ║    444   ║   333   ║          0         ║                                                               ║
╠═══════╬════════╬══════════╬═════════╬════════════════════╬═══════════════════════════════════════════════════════════════╣
║   2   ║    3   ║    333   ║   222   ║          1         ║ The writer of this thread, 222, spoke here                    ║
╠═══════╬════════╬══════════╬═════════╬════════════════════╬═══════════════════════════════════════════════════════════════╣
║   2   ║    3   ║    222   ║   333   ║          0         ║                                                               ║
╠═══════╬════════╬══════════╬═════════╬════════════════════╬═══════════════════════════════════════════════════════════════╣
║   2   ║    3   ║    444   ║   222   ║          1         ║ The writer of this thread, 222, spoke here                    ║
╠═══════╬════════╬══════════╬═════════╬════════════════════╬═══════════════════════════════════════════════════════════════╣
║   2   ║    3   ║    222   ║   444   ║          0         ║                                                               ║
╚═══════╩════════╩══════════╩═════════╩════════════════════╩═══════════════════════════════════════════════════════════════╝
在按“主题”分组后,我们可以使用match with nomatch=0

library(dplyr)
df %>%
   group_by(topic) %>% 
   mutate(write_involvement =  match(speaker, first(listener), nomatch = 0)) %>%
   ungroup
-输出

# A tibble: 10 x 5
#   topic thread listener speaker write_involvement
#   <dbl>  <dbl>    <dbl>   <dbl>             <int>
# 1     1     10      111     222                 0
# 2     1     10      111     333                 0
# 3     1     10      222     111                 1
# 4     1     10      111     444                 0
# 5     2      3      222     444                 0
# 6     2      3      444     333                 0
# 7     2      3      333     222                 1
# 8     2      3      222     333                 0
# 9     2      3      444     222                 1
#10     2      3      222     444                 0
在按“主题”分组后,我们可以使用match with nomatch=0

library(dplyr)
df %>%
   group_by(topic) %>% 
   mutate(write_involvement =  match(speaker, first(listener), nomatch = 0)) %>%
   ungroup
-输出

# A tibble: 10 x 5
#   topic thread listener speaker write_involvement
#   <dbl>  <dbl>    <dbl>   <dbl>             <int>
# 1     1     10      111     222                 0
# 2     1     10      111     333                 0
# 3     1     10      222     111                 1
# 4     1     10      111     444                 0
# 5     2      3      222     444                 0
# 6     2      3      444     333                 0
# 7     2      3      333     222                 1
# 8     2      3      222     333                 0
# 9     2      3      444     222                 1
#10     2      3      222     444                 0
使用ave的基本R选项

data.table选项

给予

使用ave的基本R选项

data.table选项

给予

within(
  df,
  writer_involvement <- +(ave(listener, topic, thread, FUN = function(x) head(x, 1)) == speaker)
)
   topic thread listener speaker writer_involvement
1      1     10      111     222                  0
2      1     10      111     333                  0
3      1     10      222     111                  1
4      1     10      111     444                  0
5      2      3      222     444                  0
6      2      3      444     333                  0
7      2      3      333     222                  1
8      2      3      222     333                  0
9      2      3      444     222                  1
10     2      3      222     444                  0
setDT(df)[, writer_involvement := +(speaker == head(listener, 1)), .(topic, thread)]
> df
    topic thread listener speaker writer_involvement
 1:     1     10      111     222                  0
 2:     1     10      111     333                  0
 3:     1     10      222     111                  1
 4:     1     10      111     444                  0
 5:     2      3      222     444                  0
 6:     2      3      444     333                  0
 7:     2      3      333     222                  1
 8:     2      3      222     333                  0
 9:     2      3      444     222                  1
10:     2      3      222     444                  0