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

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

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

问题

以下是您要翻译的部分:

嗨,大家好,我正在尝试安排重复的代码,使它们依次显示。请查看下面的代码和数据:

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

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


<details>
<summary>英文:</summary>

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


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


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

Would be ideal to not completely change my code an maybe just to it. 

</details>


# 答案1
**得分**: 2

以下是已翻译的内容:

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

```r
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 转换为因子类型并重新定义其级别:

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

# 选项 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
英文:

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

df2 &lt;- edta %&gt;%
  filter(n() &gt; 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

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

df2 %&gt;%
  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

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:

# Option 1
df2 %&gt;%
  arrange(factor(edta_code, levels = unique(edta_code)))

# Option 2
df2 %&gt;%
  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

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:

确定