R:如何使用group\u映射对group\u之后的数据帧进行排序?
鉴于此,我有一个数据帧,如下所示:R:如何使用group\u映射对group\u之后的数据帧进行排序?,r,R,鉴于此,我有一个数据帧,如下所示: dt <- data.frame(year = sample(c(2000:2019),100 ), month = sample(c(1:12),100 ), paitent_ID = sample(c(1:50),100,replace = T ) 但是,它抱怨: Error in order(year, month) : object 'year' not found 我们可以使用排列而不是顺序 li
dt <- data.frame(year = sample(c(2000:2019),100 ),
month = sample(c(1:12),100 ),
paitent_ID = sample(c(1:50),100,replace = T )
但是,它抱怨:
Error in order(year, month) : object 'year' not found
我们可以使用
排列
而不是顺序
library(dplyr)
dt %>%
group_by(paitent_ID) %>%
group_map( ~ .x %>%
arrange(year, month))
#[[1]]
# A tibble: 4 x 2
# year month
# <int> <int>
#1 2003 10
#2 2009 12
#3 2013 3
#4 2013 6
#[[2]]
# A tibble: 6 x 2
# year month
# <int> <int>
#1 2000 6
#2 2000 9
#3 2006 8
#4 2009 3
#5 2015 12
#6 2017 10
#...
库(dplyr)
dt%>%
分组依据(paitent\u ID)%>%
组映射(~.x%>%
安排(年、月)
#[[1]]
#一个tibble:4x2
#年-月
#
#1 2003 10
#2 2009 12
#3 2013 3
#4 2013 6
#[[2]]
#一个tibble:6x2
#年-月
#
#1 2000 6
#2 2000 9
#3 2006 8
#4 2009 3
#5 2015 12
#6 2017 10
#...
为什么不安排(患者ID、年、月)?
library(dplyr)
dt %>%
group_by(paitent_ID) %>%
group_map( ~ .x %>%
arrange(year, month))
#[[1]]
# A tibble: 4 x 2
# year month
# <int> <int>
#1 2003 10
#2 2009 12
#3 2013 3
#4 2013 6
#[[2]]
# A tibble: 6 x 2
# year month
# <int> <int>
#1 2000 6
#2 2000 9
#3 2006 8
#4 2009 3
#5 2015 12
#6 2017 10
#...