当列中有NAS时
我有一个纵向数据集,其中有两个人,其中数据行被编号为“情节”,有些情节具有测试“结果”。以下代码的目的是:
- 创建二进制变量“ sup”以评估“结果”。如果结果== na,则sup == na。此代码有效。
- 创建SUP_RANK以列举出现SUP == 1的人中的SUP == 1的发生。换句话说,我想知道这是否是第一次,第二次等。 问题:此代码当前不起作用,因为Person 2的第一个sup == 1被排名为'2'(当它被排名为'1'时)。
- 创建一个事件变量,该变量:
- 等于1,如果sup_rank == 1
- 等于0,如果sup == 0 或 sup_rank不等于1
- 等于na,如果sup(和sup_rank)
当前我试图做## na等于na 3分在两个步骤中,比赛和事件决赛。 问题:它不起作用,因为'sup_rank'不起作用,但是无论如何,创建“事件”是一个变量(并且不需要'event_final')是理想的选择。
#Load packages
pacman::p_load(dplyr)
#Create variables for data set
person <- c(1, 1, 2, 2, 2, 2, 2, 2, 2, 2)
episode <- c(1, 2, 1, 2, 3, 4, 5, 6, 7, 8)
result <- c(NA, NA, NA, 1, NA, 2, NA, 2, NA, 2)
#Populate data frame with variables
d <- cbind(person, episode, result)
d <- as.data.frame(d)
#Manipulate data frame to create 4 new variables
d1 <- d %>%
#Need to create new variables within each person
group_by(person) %>%
#Need to correctly order the rows of data before creating the variables
arrange(person, episode) %>%
#Create variable to evaluate 'result'
mutate(sup = if_else(result == 2, 1, 0, NA_real_)) %>%
#if sup == 1, rank it
mutate(sup_rank = ifelse(sup == 1, rank(sup == 1, na.last = 'keep', ties.method = 'first'), NA_real_)) %>%
#create an event if the rank of the sup == 1 is equal to 1 (we want the initial suppression)
mutate(event = if_else(sup_rank == 1, 1, 0, NA_real_)) %>%
#now override the value of event to be equal to 0 if sup==0
mutate(event_final = if_else(sup == 0, 0, event)) %>%
arrange(person, episode)
print(d1)
#> # A tibble: 10 x 7
#> # Groups: person [2]
#> person episode result sup sup_rank event event_final
#> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 1 1 NA NA NA NA NA
#> 2 1 2 NA NA NA NA NA
#> 3 2 1 NA NA NA NA NA
#> 4 2 2 1 0 NA NA 0
#> 5 2 3 NA NA NA NA NA
#> 6 2 4 2 1 2 0 0
#> 7 2 5 NA NA NA NA NA
#> 8 2 6 2 1 3 0 0
#> 9 2 7 NA NA NA NA NA
#> 10 2 8 2 1 4 0 0
由
I have a longitudinal data set with two people in which the rows of data are numbered as 'episodes', and some episodes have a test 'result'. The goal of the below code is to:
- Create binary variable 'sup' to evaluate a 'result'. If result == NA, then sup == NA. This code works.
- Create sup_rank to enumerate the occurrence of sup == 1 within people who had an occurrence of sup==1. In other words, I want to know if this is the first time, second time, etc. that sup==1. Problem: This code currently does not work since person 2's first sup==1 is ranked as '2' (when it should be ranked as '1').
- Create an event variable that:
- equals 1 if sup_rank==1
- equals 0 if sup == 0 OR sup_rank does not equal 1
- equals NA if sup (and thus sup_rank) equals NA
Currently I tried to do #3 in two steps with event and event final. Problem: it does not work because 'sup_rank' does not work, but regardless, it would be ideal to create 'event' as one variable (and not need an 'event_final').
#Load packages
pacman::p_load(dplyr)
#Create variables for data set
person <- c(1, 1, 2, 2, 2, 2, 2, 2, 2, 2)
episode <- c(1, 2, 1, 2, 3, 4, 5, 6, 7, 8)
result <- c(NA, NA, NA, 1, NA, 2, NA, 2, NA, 2)
#Populate data frame with variables
d <- cbind(person, episode, result)
d <- as.data.frame(d)
#Manipulate data frame to create 4 new variables
d1 <- d %>%
#Need to create new variables within each person
group_by(person) %>%
#Need to correctly order the rows of data before creating the variables
arrange(person, episode) %>%
#Create variable to evaluate 'result'
mutate(sup = if_else(result == 2, 1, 0, NA_real_)) %>%
#if sup == 1, rank it
mutate(sup_rank = ifelse(sup == 1, rank(sup == 1, na.last = 'keep', ties.method = 'first'), NA_real_)) %>%
#create an event if the rank of the sup == 1 is equal to 1 (we want the initial suppression)
mutate(event = if_else(sup_rank == 1, 1, 0, NA_real_)) %>%
#now override the value of event to be equal to 0 if sup==0
mutate(event_final = if_else(sup == 0, 0, event)) %>%
arrange(person, episode)
print(d1)
#> # A tibble: 10 x 7
#> # Groups: person [2]
#> person episode result sup sup_rank event event_final
#> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 1 1 NA NA NA NA NA
#> 2 1 2 NA NA NA NA NA
#> 3 2 1 NA NA NA NA NA
#> 4 2 2 1 0 NA NA 0
#> 5 2 3 NA NA NA NA NA
#> 6 2 4 2 1 2 0 0
#> 7 2 5 NA NA NA NA NA
#> 8 2 6 2 1 3 0 0
#> 9 2 7 NA NA NA NA NA
#> 10 2 8 2 1 4 0 0
Created on 2022-04-20 by the reprex package (v2.0.0)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
有一种更有效的方法可以肯定地做到这一点,但是与此同时,这是我创建的解决方案:
由 reprex软件包(v2.0.0)
There is a more efficient way to do this for sure, but in the meantime, here's a solution I created:
Created on 2022-04-22 by the reprex package (v2.0.0)