如何安排重复的示例代码以在输出中按顺序排列

huangapple go评论101阅读模式
英文:

How to arrange duplicated sample codes to follow on another in an output

问题

以下是您要翻译的部分:

  1. 嗨,大家好,我正在尝试安排重复的代码,使它们依次显示。请查看下面的代码和数据:
  2. df1 <- structure(list(
  3. 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"),
  4. edta_collect = c(1, 0, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1),
  5. edta_code = c("EDTA45", NA, "EDTA20", "EDTA66", "EDTA12", NA, NA, "EDTA19", "EDTA03", "EDTA66", "EDTA10", "EDTA03", "EDTA30", "EDTA20"),
  6. ipv = c(1, 1, 4, 6, 3, 2, 5, 1, 3, 4, 5, 2),
  7. epds = c(13, 12, 10, 8, 30, 33, 20, 26, 12, 10, 11, 15, 1, 13, 40)),
  8. class = "data.frame", row.names = c(NA, -14L))
  9. edta <- df1 %>%
  10. select(subject_id, edta_collect, edta_code) %>%
  11. filter(edta_collect == 1)
  12. n_occur_edta <- data.frame(table(edta$edta_code))
  13. edta[edta$edta_code %in% n_occur_edta$Var1[n_occur_edta$Freq > 1], ]

当前输出:

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

期望的输出:

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

最好不要完全更改我的代码,也许只需对其进行修改就可以了。

  1. <details>
  2. <summary>英文:</summary>
  3. Hi all I am trying to arrange duplicated codes so they show up after one another. Please see the code and data below:

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], ]

  1. Current output:

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

  1. Desired output:

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

  1. Would be ideal to not completely change my code an maybe just to it.
  2. </details>
  3. # 答案1
  4. **得分**: 2
  5. 以下是已翻译的内容:
  6. 在更多的 `dplyr` 意义上,我将通过以下方式生成您当前的输出:
  7. ```r
  8. df2 <- edta %>%
  9. filter(n() > 1, .by = edta_code)
  10. df2
  11. # subject_id edta_collect edta_code
  12. # 1 191-3457 1 EDTA20
  13. # 2 191-0987 1 EDTA66
  14. # 3 191-9600 1 EDTA03
  15. # 4 191-0001 1 EDTA66
  16. # 5 191-0003 1 EDTA03
  17. # 6 191-5000 1 EDTA20

如果您只想按 edta_code 的字母顺序对数据进行排序,您可以仅使用 arrange()

  1. df2 %>%
  2. arrange(edta_code)
  3. # subject_id edta_collect edta_code
  4. # 1 191-9600 1 EDTA03
  5. # 2 191-0003 1 EDTA03
  6. # 3 191-3457 1 EDTA20
  7. # 4 191-5000 1 EDTA20
  8. # 5 191-0987 1 EDTA66
  9. # 6 191-0001 1 EDTA66

如果您需要按它们首次出现的顺序排列,您可以将 edta_code 转换为因子类型并重新定义其级别:

  1. # 选项 1
  2. df2 %>%
  3. arrange(factor(edta_code, levels = unique(edta_code)))
  4. # 选项 2
  5. df2 %>%
  6. arrange(forcats::fct_inorder(edta_code))
  7. # subject_id edta_collect edta_code
  8. # 1 191-3457 1 EDTA20
  9. # 2 191-5000 1 EDTA20
  10. # 3 191-0987 1 EDTA66
  11. # 4 191-0001 1 EDTA66
  12. # 5 191-9600 1 EDTA03
  13. # 6 191-0003 1 EDTA03
英文:

In the more dplyr sense, I will generate your current output by the following way:

  1. df2 &lt;- edta %&gt;%
  2. filter(n() &gt; 1, .by = edta_code)
  3. df2
  4. # subject_id edta_collect edta_code
  5. # 1 191-3457 1 EDTA20
  6. # 2 191-0987 1 EDTA66
  7. # 3 191-9600 1 EDTA03
  8. # 4 191-0001 1 EDTA66
  9. # 5 191-0003 1 EDTA03
  10. # 6 191-5000 1 EDTA20

If you simply want to sort the data by the alphabetical order of edta_code, you can solely use arrange():

  1. df2 %&gt;%
  2. arrange(edta_code)
  3. # subject_id edta_collect edta_code
  4. # 1 191-9600 1 EDTA03
  5. # 2 191-0003 1 EDTA03
  6. # 3 191-3457 1 EDTA20
  7. # 4 191-5000 1 EDTA20
  8. # 5 191-0987 1 EDTA66
  9. # 6 191-0001 1 EDTA66

If you have to arrange it by the order in which they first appear, you can convert edta_code into the factor type and redefine its levels:

  1. # Option 1
  2. df2 %&gt;%
  3. arrange(factor(edta_code, levels = unique(edta_code)))
  4. # Option 2
  5. df2 %&gt;%
  6. arrange(forcats::fct_inorder(edta_code))
  7. # subject_id edta_collect edta_code
  8. # 1 191-3457 1 EDTA20
  9. # 2 191-5000 1 EDTA20
  10. # 3 191-0987 1 EDTA66
  11. # 4 191-0001 1 EDTA66
  12. # 5 191-9600 1 EDTA03
  13. # 6 191-0003 1 EDTA03

huangapple
  • 本文由 发表于 2023年7月18日 15:32:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/76710442.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定