R语言 如何安排重复的示例代码在输出中跟随另一个

zdwk9cvp  于 2023-07-31  发布在  其他
关注(0)|答案(1)|浏览(98)

大家好,我正试图安排重复的代码,使他们出现在一个又一个。请看下面的代码和数据:

df1 <- structure (list(
  subject_id = c("191-5467", "191-6784", "191-3457", "191-0987", "191-1245", "191-1945", "191-3000", "191-5000", "191-9600", "191-0001", "191-0002", "191-0003", "191-0004", "191-5000"), 
  edta_collect = c(1,0,1,1,1,0,0,1,1,1,1,1,1,1),
  edta_code = c("EDTA45", NA, "EDTA20", "EDTA66", "EDTA12", NA,NA,"EDTA19", "EDTA03", "EDTA66", "EDTA10", "EDTA03", "EDTA30", "EDTA20"), 
  ipv = c(1,1,4,6,3,2,5,1,3,4,5,2), 
  epds = c(13, 12, 10, 8, 30, 33, 20, 26, 12, 10, 11, 15, 1, 13, 40)), 
  class = "data.frame", row.names = c(NA, -14L))
edta <- df1 %>%
  select(subject_id, edta_collect, edta_code) %>%
  filter(edta_collect == 1)

n_occur_edta <- data.frame(table(edta$edta_code))

edta[edta$edta_code %in% n_occur_edta$Var1[n_occur_edta$Freq > 1], ]

电流输出:

subject_id edta_collect edta_code
2    191-3457            1    EDTA20
3    191-0987            1    EDTA66
6    191-9600            1    EDTA03
7    191-0001            1    EDTA66
9    191-0003            1    EDTA03
11   191-5000            1    EDTA20


所需输出:

subject_id edta_collect edta_code
2    191-3457            1    EDTA20
11   191-5000            1    EDTA20
3    191-0987            1    EDTA66
7    191-0001            1    EDTA66
6    191-9600            1    EDTA03
9    191-0003            1    EDTA03


最好不要完全改变我的代码,也许只是改变它。

hk8txs48

hk8txs481#

在更dplyr的意义上,我将通过以下方式生成您的当前输出:

df2 <- edta %>%
  filter(n() > 1, .by = edta_code)

df2
#   subject_id edta_collect edta_code
# 1   191-3457            1    EDTA20
# 2   191-0987            1    EDTA66
# 3   191-9600            1    EDTA03
# 4   191-0001            1    EDTA66
# 5   191-0003            1    EDTA03
# 6   191-5000            1    EDTA20

字符串
如果你只是想按照字母顺序edta_code对数据进行排序,你可以单独使用arrange()

df2 %>%
  arrange(edta_code)

#   subject_id edta_collect edta_code
# 1   191-9600            1    EDTA03
# 2   191-0003            1    EDTA03
# 3   191-3457            1    EDTA20
# 4   191-5000            1    EDTA20
# 5   191-0987            1    EDTA66
# 6   191-0001            1    EDTA66


如果您必须按它们首次出现的顺序排列,则可以将edta_code转换为因子类型并重新定义其水平:

# Option 1
df2 %>%
  arrange(factor(edta_code, levels = unique(edta_code)))

# Option 2
df2 %>%
  arrange(forcats::fct_inorder(edta_code))

#   subject_id edta_collect edta_code
# 1   191-3457            1    EDTA20
# 2   191-5000            1    EDTA20
# 3   191-0987            1    EDTA66
# 4   191-0001            1    EDTA66
# 5   191-9600            1    EDTA03
# 6   191-0003            1    EDTA03

相关问题