我有
的数据subject <- c(1,1,1,2,2,3,3,3)
day <- c(1,2,3,1,2,1,2,3)
RRT <- c(0,0,1,1,0,0,1,1)
SOFA <- c(8,9,2,10,12,11,19,8)
libo <- data.frame(subject,day,RRT,SOFA)
我只想从第1天RRT = 0的患者中选择数据。 我想要的输出是
subject <- c(1,1,1,3,3,3)
day <- c(1,2,3,1,2,3)
RRT <- c(0,0,1,0,1,1)
SOFA <- c(8,9,2,11,19,8)
libodesired <- data.frame(subject,day,RRT, SOFA)
如何在不改变长格式的情况下执行此操作?是否可以使用dplyr?
答案 0 :(得分:0)
您可以使用双subset
:
subset(libo,
subject %in% subset(libo, RRT == 0 & day == 1)$subject)
subject day RRT SOFA
1 1 1 0 8
2 1 2 0 9
3 1 3 1 2
6 3 1 0 11
7 3 2 1 19
8 3 3 1 8
subjects
和RRT
(day
)subset(libo, RRT == 0 & day == 1)$subject
subset(libo %in% SUBJECTS)
答案 1 :(得分:0)
使用tidyverse
和dplyr
,您可以根据自己的条件选择主题并加入到表格的子集
subject<-c(1,1,1,2,2,3,3,3)
day<-c(1,2,3,1,2,1,2,3)
RRT<-c(0,0,1,1,0,0,1,1)
SOFA<-c(8,9,2,10,12,11,19,8)
libo<-data.frame(subject,day,RRT,SOFA)
library(dplyr, warn.conflicts = TRUE)
libo %>%
# select base on your criteria
group_by(subject) %>%
filter(RRT == 0 & day == 1) %>%
select(subject) %>%
# only keep rows from libo based on your subjects selection
left_join(libo, by = "subject")
#> # A tibble: 6 x 4
#> # Groups: subject [?]
#> subject day RRT SOFA
#> <dbl> <dbl> <dbl> <dbl>
#> 1 1 1 0 8
#> 2 1 2 0 9
#> 3 1 3 1 2
#> 4 3 1 0 11
#> 5 3 2 1 19
#> 6 3 3 1 8