在R中,通过”S.NO”分组收集数据时,在数据的开头和结尾添加”Age”的NA值。

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

Adding NA for Age at the start and end of the data collected when grouped by S.NO in R

问题

S.NO AGE_1 AGE_2
123 NA 19
123 19 24
123 24 NA
124 NA 18
124 18 21
124 21 28
124 28 35
124 35 NA
125 NA 13
125 13 19
125 19 23
125 23 NA
126 NA 19
126 19 21
126 21 NA
英文:

I have a dataset that looks like this and I am trying to add NA at the beginning and end of the age data collected for each S.NO. as shown in the expected dataset in R.
Would appreciate any help. Thank you.

S.NO AGE_1 AGE_2
123 19 24
124 18 21
124 21 28
124 28 35
125 13 19
125 19 23
126 19 21

Expected Dataset

S.NO AGE_1 AGE_2
123 NA 19
123 19 24
123 24 NA
124 NA 18
124 18 21
124 21 28
124 28 35
124 35 NA
125 NA 13
125 13 19
125 19 23
125 23 NA
126 NA 19
126 19 21
126 21 NA

答案1

得分: 1

使用 tibble::add_rowdplyr::group_splitpurrr::map_dfr,您可以执行以下操作:

  1. library(dplyr, warn = FALSE)
  2. library(tibble)
  3. library(purrr)
  4. pad_rows <- function(x) {
  5. x |>
  6. tibble::add_row(S.NO = first(x$S.NO), AGE_2 = first(x$AGE_1), .before = 1) |>
  7. tibble::add_row(S.NO = last(x$S.NO), AGE_1 = last(x$AGE_2), .after = Inf)
  8. }
  9. dat |>
  10. group_split(S.NO) |>
  11. purrr::map_dfr(pad_rows)
  12. #> # A tibble: 15 × 3
  13. #> S.NO AGE_1 AGE_2
  14. #> <int> <int> <int>
  15. #> 1 123 NA 19
  16. #> 2 123 19 24
  17. #> 3 123 24 NA
  18. #> 4 124 NA 18
  19. #> 5 124 18 21
  20. #> 6 124 21 28
  21. #> 7 124 28 35
  22. #> 8 124 35 NA
  23. #> 9 125 NA 13
  24. #> 10 125 13 19
  25. #> 11 125 19 23
  26. #> 12 125 23 NA
  27. #> 13 126 NA 19
  28. #> 14 126 19 21
  29. #> 15 126 21 NA

数据

  1. dat <- data.frame(
  2. S.NO = c(123L, 124L, 124L, 124L, 125L, 125L, 126L),
  3. AGE_1 = c(19L, 18L, 21L, 28L, 13L, 19L, 19L),
  4. AGE_2 = c(24L, 21L, 28L, 35L, 19L, 23L, 21L)
  5. )
英文:

Using tibble::add_row, dplyr::group_split and purrr::map_dfr you could do:

  1. library(dplyr, warn = FALSE)
  2. library(tibble)
  3. library(purrr)
  4. pad_rows &lt;- function(x) {
  5. x |&gt;
  6. tibble::add_row(S.NO = first(x$S.NO), AGE_2 = first(x$AGE_1), .before = 1) |&gt;
  7. tibble::add_row(S.NO = last(x$S.NO), AGE_1 = last(x$AGE_2), .after = Inf)
  8. }
  9. dat |&gt;
  10. group_split(S.NO) |&gt;
  11. purrr::map_dfr(pad_rows)
  12. #&gt; # A tibble: 15 &#215; 3
  13. #&gt; S.NO AGE_1 AGE_2
  14. #&gt; &lt;int&gt; &lt;int&gt; &lt;int&gt;
  15. #&gt; 1 123 NA 19
  16. #&gt; 2 123 19 24
  17. #&gt; 3 123 24 NA
  18. #&gt; 4 124 NA 18
  19. #&gt; 5 124 18 21
  20. #&gt; 6 124 21 28
  21. #&gt; 7 124 28 35
  22. #&gt; 8 124 35 NA
  23. #&gt; 9 125 NA 13
  24. #&gt; 10 125 13 19
  25. #&gt; 11 125 19 23
  26. #&gt; 12 125 23 NA
  27. #&gt; 13 126 NA 19
  28. #&gt; 14 126 19 21
  29. #&gt; 15 126 21 NA

DATA

  1. dat &lt;- data.frame(
  2. S.NO = c(123L, 124L, 124L, 124L, 125L, 125L, 126L),
  3. AGE_1 = c(19L, 18L, 21L, 28L, 13L, 19L, 19L),
  4. AGE_2 = c(24L, 21L, 28L, 35L, 19L, 23L, 21L)
  5. )

答案2

得分: 1

使用 group_modify

  1. library(dplyr)
  2. library(tibble)
  3. df %>%
  4. group_by(S.NO) %>%
  5. group_modify(~ .x %>%
  6. add_row(AGE_2 = first(.x$AGE_1), .before = 1) %>%
  7. add_row(AGE_1 = last(.x$AGE_2))) %>%
  8. ungroup

-输出

  1. # A tibble: 15 × 3
  2. S.NO AGE_1 AGE_2
  3. <int> <int> <int>
  4. 1 123 NA 19
  5. 2 123 19 24
  6. 3 123 24 NA
  7. 4 124 NA 18
  8. 5 124 18 21
  9. 6 124 21 28
  10. 7 124 28 35
  11. 8 124 35 NA
  12. 9 125 NA 13
  13. 10 125 13 19
  14. 11 125 19 23
  15. 12 125 23 NA
  16. 13 126 NA 19
  17. 14 126 19 21
  18. 15 126 21 NA
英文:

Using group_modify

  1. library(dplyr)
  2. library(tibble)
  3. df %&gt;%
  4. group_by(S.NO) %&gt;%
  5. group_modify(~ .x %&gt;%
  6. add_row(AGE_2= first(.x$AGE_1), .before = 1) %&gt;%
  7. add_row(AGE_1 = last(.x$AGE_2))) %&gt;%
  8. ungroup

-output

  1. # A tibble: 15 &#215; 3
  2. S.NO AGE_1 AGE_2
  3. &lt;int&gt; &lt;int&gt; &lt;int&gt;
  4. 1 123 NA 19
  5. 2 123 19 24
  6. 3 123 24 NA
  7. 4 124 NA 18
  8. 5 124 18 21
  9. 6 124 21 28
  10. 7 124 28 35
  11. 8 124 35 NA
  12. 9 125 NA 13
  13. 10 125 13 19
  14. 11 125 19 23
  15. 12 125 23 NA
  16. 13 126 NA 19
  17. 14 126 19 21
  18. 15 126 21 NA

答案3

得分: 0

使用nest_by的方法:

  1. library(dplyr)
  2. library(tidyr) # unnest
  3. df %>%
  4. nest_by(S.NO) %>%
  5. mutate(data = list(unique(unlist(data))) %>%
  6. unnest(data) %>%
  7. group_by(S.NO) %>%
  8. reframe(AGE_1 = c(NA, data), AGE_2 = c(data, NA))
  9. # A tibble: 15 × 3
  10. S.NO AGE_1 AGE_2
  11. <int> <int> <int>
  12. 1 123 NA 19
  13. 2 123 19 24
  14. 3 123 24 NA
  15. 4 124 NA 18
  16. 5 124 18 21
  17. 6 124 21 28
  18. 7 124 28 35
  19. 8 124 35 NA
  20. 9 125 NA 13
  21. 10 125 13 19
  22. 11 125 19 23
  23. 12 125 23 NA
  24. 13 126 NA 19
  25. 14 126 19 21
  26. 15 126 21 NA

数据

  1. df <- structure(list(S.NO = c(123L, 124L, 124L, 124L, 125L, 125L, 126L
  2. ), AGE_1 = c(19L, 18L, 21L, 28L, 13L, 19L, 19L), AGE_2 = c(24L,
  3. 21L, 28L, 35L, 19L, 23L, 21L)), class = "data.frame", row.names = c(NA,
  4. -7L))
英文:

An approach using nest_by

  1. library(dplyr)
  2. library(tidyr) # unnest
  3. df %&gt;%
  4. nest_by(S.NO) %&gt;%
  5. mutate(data = list(unique(unlist(data)))) %&gt;%
  6. unnest(data) %&gt;%
  7. group_by(S.NO) %&gt;%
  8. reframe(AGE_1 = c(NA, data), AGE_2 = c(data, NA))
  9. # A tibble: 15 &#215; 3
  10. S.NO AGE_1 AGE_2
  11. &lt;int&gt; &lt;int&gt; &lt;int&gt;
  12. 1 123 NA 19
  13. 2 123 19 24
  14. 3 123 24 NA
  15. 4 124 NA 18
  16. 5 124 18 21
  17. 6 124 21 28
  18. 7 124 28 35
  19. 8 124 35 NA
  20. 9 125 NA 13
  21. 10 125 13 19
  22. 11 125 19 23
  23. 12 125 23 NA
  24. 13 126 NA 19
  25. 14 126 19 21
  26. 15 126 21 NA

Data

  1. df &lt;- structure(list(S.NO = c(123L, 124L, 124L, 124L, 125L, 125L, 126L
  2. ), AGE_1 = c(19L, 18L, 21L, 28L, 13L, 19L, 19L), AGE_2 = c(24L,
  3. 21L, 28L, 35L, 19L, 23L, 21L)), class = &quot;data.frame&quot;, row.names = c(NA,
  4. -7L))

huangapple
  • 本文由 发表于 2023年2月7日 02:25:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/75365207.html
匿名

发表评论

匿名网友

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

确定