将具有不规则名称的列变为行。

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

pivot columns with irregular names into rows

问题

  1. df2 <- structure(list(A = c("A_1_01", "A_1_01", "A_1_01"), B = c("A", "A", "A"),
  2. C = c("1", "1", "1"), D = c("inside", "eating", "sleeping"),
  3. `1` = c("1", "1", "0"), `2` = c("1", "0", "0"), `3` = c("0", "0", "1"),
  4. `4` = c("0", "1", "1"), `1_Location` = c("I", "I", "I"), `2_Location` = c("I", "I", "I"),
  5. `3_Location` = c("O", "O", "O"), `4_Location` = c("O", "O", "O")),
  6. class = "data.frame", row.names = c(NA, -3L))
  7. df3 <- structure(list(H = c("1", "2", "3", "4", "1", "2", "3", "4", "1", "2", "3", "4"),
  8. A = c("A_1_01", "A_1_01", "A_1_01", "A_1_01", "A_1_01", "A_1_01", "A_1_01", "A_1_01", "A_1_01", "A_1_01", "A_1_01", "A_1_01"),
  9. B = c("A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A"),
  10. C = c("1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1"),
  11. D = c("inside", "inside", "inside", "inside", "eating", "eating", "eating", "eating", "sleeping", "sleeping", "sleeping", "sleeping"),
  12. Value = c(1, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 1),
  13. Location = c("I", "I", "O", "O", "I", "I", "O", "O", "I", "I", "O", "O")),
  14. class = "data.frame", row.names = c(NA, -12L))
英文:

Using R how do I pivot the columns into rows to get the required structure in df3 from df2, while extracting new information from the previous dataframe:

  1. df2&lt;- structure(list(A=c(&quot;A_1_01&quot;, &quot;A_1_01&quot;, &quot;A_1_01&quot;), B=c(&quot;A&quot;, &quot;A&quot;, &quot;A&quot;), C=c(&quot;1&quot;, &quot;1&quot;, &quot;1&quot;), D=c(&quot;inside&quot;, &quot;eating&quot;, &quot;sleeping&quot;), &quot;1&quot;=c(&quot;1&quot;,&quot;1&quot;,&quot;0&quot;), &quot;2&quot;=c(&quot;1&quot;,&quot;0&quot;,&quot;0&quot;), &quot;3&quot;=c(&quot;0&quot;,&quot;0&quot;,&quot;1&quot;), &quot;4&quot;=c(&quot;0&quot;,&quot;1&quot;,&quot;1&quot;), &quot;1_Location&quot;=c(&quot;I&quot;,&quot;I&quot;, &quot;I&quot;), &quot;2_Location&quot;=c(&quot;I&quot;,&quot;I&quot;, &quot;I&quot;), &quot;3_Location&quot;=c(&quot;O&quot;,&quot;O&quot;, &quot;O&quot;), &quot;4_Location&quot;=c(&quot;O&quot;,&quot;O&quot;, &quot;O&quot;)), class= &quot;data.frame&quot;, row.names = c(NA,-3L))
  1. df3&lt;- structure(list(H=c(&quot;1&quot;,&quot;2&quot;,&quot;3&quot;,&quot;4&quot;,&quot;1&quot;,&quot;2&quot;,&quot;3&quot;,&quot;4&quot;,&quot;1&quot;,&quot;2&quot;,&quot;3&quot;,&quot;4&quot;),
  2. A=c(&quot;A_1_01&quot;, &quot;A_1_01&quot;, &quot;A_1_01&quot;,&quot;A_1_01&quot;, &quot;A_1_01&quot;,
  3. &quot;A_1_01&quot;,&quot;A_1_01&quot;, &quot;A_1_01&quot;, &quot;A_1_01&quot;,&quot;A_1_01&quot;,
  4. &quot;A_1_01&quot;, &quot;A_1_01&quot;),
  5. B=c(&quot;A&quot;, &quot;A&quot;, &quot;A&quot;,&quot;A&quot;, &quot;A&quot;, &quot;A&quot;,&quot;A&quot;, &quot;A&quot;, &quot;A&quot;,&quot;A&quot;, &quot;A&quot;, &quot;A&quot;),
  6. C=c(&quot;1&quot;, &quot;1&quot;, &quot;1&quot;,&quot;1&quot;, &quot;1&quot;, &quot;1&quot;,&quot;1&quot;, &quot;1&quot;, &quot;1&quot;,&quot;1&quot;, &quot;1&quot;, &quot;1&quot;),
  7. D=c(&quot;inside&quot;,&quot;inside&quot;,&quot;inside&quot;,&quot;inside&quot;,
  8. &quot;eating&quot;,&quot;eating&quot;,&quot;eating&quot;,&quot;eating&quot;,
  9. &quot;sleeping&quot;,&quot;sleeping&quot;,&quot;sleeping&quot;,&quot;sleeping&quot;),
  10. Value=c(1,1,0,0,1,0,0,1,0,0,1,1),
  11. Location=c(&quot;I&quot;,&quot;I&quot;,&quot;O&quot;,&quot;O&quot;,&quot;I&quot;,&quot;I&quot;,&quot;O&quot;,&quot;O&quot;,&quot;I&quot;,&quot;I&quot;,&quot;O&quot;,&quot;O&quot;)),
  12. class= &quot;data.frame&quot;, row.names = c(NA,-12L))

Thank you

答案1

得分: 3

尝试在仅包含数字的列名称后添加后缀“_Value”后,使用pivot_longer函数进行操作:

  1. library(stringr)
  2. library(tidyr)
  3. library(dplyr)
  4. df2 %>%
  5. rename_with(~ str_c(.x, "_Value"), matches("^\\d+$")) %>%
  6. pivot_longer(cols = contains("_"), names_to = c("H", ".value"),
  7. names_pattern = "(\\d+)_(.*)")

-output

  1. # A tibble: 12 × 7
  2. A B C D H Value Location
  3. <chr> <chr> <chr> <chr> <chr> <chr> <chr>
  4. 1 A_1_01 A 1 inside 1 1 I
  5. 2 A_1_01 A 1 inside 2 1 I
  6. 3 A_1_01 A 1 inside 3 0 O
  7. 4 A_1_01 A 1 inside 4 0 O
  8. 5 A_1_01 A 1 eating 1 1 I
  9. 6 A_1_01 A 1 eating 2 0 I
  10. 7 A_1_01 A 1 eating 3 0 O
  11. 8 A_1_01 A 1 eating 4 1 O
  12. 9 A_1_01 A 1 sleeping 1 0 I
  13. 10 A_1_01 A 1 sleeping 2 0 I
  14. 11 A_1_01 A 1 sleeping 3 1 O
  15. 12 A_1_01 A 1 sleeping 4 1 O
英文:

Try with pivot_longer after adding a suffix '_Value' to the digit only column names

  1. library(stringr)
  2. library(tidyr)
  3. library(dplyr)
  4. df2 %&gt;%
  5. rename_with(~ str_c(.x, &quot;_Value&quot;), matches(&quot;^\\d+$&quot;)) %&gt;%
  6. pivot_longer(cols = contains(&quot;_&quot;), names_to = c(&quot;H&quot;, &quot;.value&quot;),
  7. names_pattern = &quot;(\\d+)_(.*)&quot;)

-output

  1. # A tibble: 12 &#215; 7
  2. A B C D H Value Location
  3. &lt;chr&gt; &lt;chr&gt; &lt;chr&gt; &lt;chr&gt; &lt;chr&gt; &lt;chr&gt; &lt;chr&gt;
  4. 1 A_1_01 A 1 inside 1 1 I
  5. 2 A_1_01 A 1 inside 2 1 I
  6. 3 A_1_01 A 1 inside 3 0 O
  7. 4 A_1_01 A 1 inside 4 0 O
  8. 5 A_1_01 A 1 eating 1 1 I
  9. 6 A_1_01 A 1 eating 2 0 I
  10. 7 A_1_01 A 1 eating 3 0 O
  11. 8 A_1_01 A 1 eating 4 1 O
  12. 9 A_1_01 A 1 sleeping 1 0 I
  13. 10 A_1_01 A 1 sleeping 2 0 I
  14. 11 A_1_01 A 1 sleeping 3 1 O
  15. 12 A_1_01 A 1 sleeping 4 1 O

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

发表评论

匿名网友

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

确定