英文:
Copy values to rows based on conditions
问题
我正在尝试基于它们匹配的案例的索引日期复制控件的数据集的索引日期变量。在这个数据中,case = 1,control = 0。每对在"matchid"列中都有一个唯一的ID,时间=timepoint。我有以下示例数据集:
Study_ID time index_date case matchid
<chr> <dbl> <dbl> <dbl> <dbl>
1 101 0 2 1 1
2 101 1 2 1 1
3 101 2 2 1 1
4 101 3 2 1 1
5 340 0 NA 0 1
6 340 1 NA 0 1
7 340 2 NA 0 1
8 340 3 NA 0 1
我需要将行5-8的index_date列设置为"2",基于"matchid"相同,使其看起来像下面这样:
Study_ID time index_date case matchid
<chr> <dbl> <dbl> <dbl> <dbl>
1 101 0 2 1 1
2 101 1 2 1 1
3 101 2 2 1 1
4 101 3 2 1 1
5 340 0 2 0 1
6 340 1 2 0 1
7 340 2 2 0 1
8 340 3 2 0 1
非常感谢您的帮助,因为类似问题的解决方案没有解决我的问题。
我已经尝试了以下Stack Overflow解决方案,但我收到了错误信息。
英文:
I have a dataset that I am trying to copy an index date variable for controls based on their matched case's index date. In this data, case = 1, control = 0. Each pair has a unique ID in the "matchid" column and time = the timepoint. I have the below sample dataset:
Study_ID time index_date case matchid
<chr> <dbl> <dbl> <dbl> <dbl>
1 101 0 2 1 1
2 101 1 2 1 1
3 101 2 2 1 1
4 101 3 2 1 1
5 340 0 NA 0 1
6 340 1 NA 0 1
7 340 2 NA 0 1
8 340 3 NA 0 1
I need the index_date column for rows 5-8 to be "2" based on "matchid" being the same so it would look like the below:
Study_ID time index_date case matchid
<chr> <dbl> <dbl> <dbl> <dbl>
1 101 0 2 1 1
2 101 1 2 1 1
3 101 2 2 1 1
4 101 3 2 1 1
5 340 0 2 0 1
6 340 1 2 0 1
7 340 2 2 0 1
8 340 3 2 0 1
Any help would be greatly appreciated as the solution for a similar question did not resolve my issue.
I have tried the below Stack Overflow solutions but I am getting errors.
https://stackoverflow.com/questions/33998856/r-copy-value-based-on-match-in-another-column
答案1
得分: 1
fill
应该执行的操作是:
library(tidyverse)
s = 'Study_ID time index_date case matchid
1 101 0 2 1 1
2 101 1 2 1 1
3 101 2 2 1 1
4 101 3 2 1 1
5 340 0 NA 0 1
6 340 1 NA 0 1
7 340 2 NA 0 1
8 340 3 NA 0 1'
t = read.table(text = s)
t %>%
group_by(matchid) %>%
fill(index_date, .direction = 'down')
# Study_ID time index_date case matchid
# <int> <int> <int> <int> <int>
# 1 101 0 2 1 1
# 2 101 1 2 1 1
# 3 101 2 2 1 1
# 4 101 3 2 1 1
# 5 340 0 2 0 1
# 6 340 1 2 0 1
# 7 340 2 2 0 1
# 8 340 3 2 0 1
英文:
fill
should do:
library(tidyverse)
s = 'Study_ID time index_date case matchid
1 101 0 2 1 1
2 101 1 2 1 1
3 101 2 2 1 1
4 101 3 2 1 1
5 340 0 NA 0 1
6 340 1 NA 0 1
7 340 2 NA 0 1
8 340 3 NA 0 1'
t = read.table(text = s)
t %>%
group_by(matchid) %>%
fill(index_date, .direction = 'down')
# Study_ID time index_date case matchid
# <int> <int> <int> <int> <int>
# 1 101 0 2 1 1
# 2 101 1 2 1 1
# 3 101 2 2 1 1
# 4 101 3 2 1 1
# 5 340 0 2 0 1
# 6 340 1 2 0 1
# 7 340 2 2 0 1
# 8 340 3 2 0 1
答案2
得分: 0
或许是这样的?
library(dplyr)
quux %>%
mutate(
index_date = if_else(is.na(index_date), na.omit(index_date)[1], index_date),
.by = c(matchid, time)
)
# Study_ID time index_date case matchid
# 1 101 0 2 1 1
# 2 101 1 2 1 1
# 3 101 2 2 1 1
# 4 101 3 2 1 1
# 5 340 0 2 0 1
# 6 340 1 2 0 1
# 7 340 2 2 0 1
# 8 340 3 2 0 1
(注意:需要使用dplyr_1.1或更新版本才支持.by=
;如果您使用较旧版本,请在mutate之前使用group_by(matchid, time)
。)
我推测我们需要做的是将index_date
中的所有NA
值替换为每个由matchid
和time
定义的分组中第一个非NA
值。
英文:
Perhaps this?
library(dplyr)
quux %>%
mutate(
index_date = if_else(is.na(index_date), na.omit(index_date)[1], index_date),
.by = c(matchid, time)
)
# Study_ID time index_date case matchid
# 1 101 0 2 1 1
# 2 101 1 2 1 1
# 3 101 2 2 1 1
# 4 101 3 2 1 1
# 5 340 0 2 0 1
# 6 340 1 2 0 1
# 7 340 2 2 0 1
# 8 340 3 2 0 1
(Note: .by=
needs dplyr_1.1 or newer; if you have older, pre-use group_by(matchid, time)
before the mutate.)
I'm inferring that what we need to do is replace all NA
values with the first non-NA
found in index_date
within each group defined by matchid
and time
.
Data
quux <- structure(list(Study_ID = c(101L, 101L, 101L, 101L, 340L, 340L, 340L, 340L), time = c(0L, 1L, 2L, 3L, 0L, 1L, 2L, 3L), index_date = c(2L, 2L, 2L, 2L, NA, NA, NA, NA), case = c(1L, 1L, 1L, 1L, 0L, 0L, 0L, 0L), matchid = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L)), class = "data.frame", row.names = c("1", "2", "3", "4", "5", "6", "7", "8"))
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论