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

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

pivot columns with irregular names into rows

问题

df2 <- structure(list(A = c("A_1_01", "A_1_01", "A_1_01"), B = c("A", "A", "A"), 
C = c("1", "1", "1"), D = c("inside", "eating", "sleeping"), 
`1` = c("1", "1", "0"), `2` = c("1", "0", "0"), `3` = c("0", "0", "1"), 
`4` = c("0", "1", "1"), `1_Location` = c("I", "I", "I"), `2_Location` = c("I", "I", "I"), 
`3_Location` = c("O", "O", "O"), `4_Location` = c("O", "O", "O")), 
class = "data.frame", row.names = c(NA, -3L))

df3 <- structure(list(H = c("1", "2", "3", "4", "1", "2", "3", "4", "1", "2", "3", "4"), 
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"), 
B = c("A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A"), 
C = c("1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1"), 
D = c("inside", "inside", "inside", "inside", "eating", "eating", "eating", "eating", "sleeping", "sleeping", "sleeping", "sleeping"), 
Value = c(1, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 1), 
Location = c("I", "I", "O", "O", "I", "I", "O", "O", "I", "I", "O", "O")), 
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:

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))
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;),
                     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;,
                         &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;,
                         &quot;A_1_01&quot;, &quot;A_1_01&quot;),
                     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;),
                     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;),
                     D=c(&quot;inside&quot;,&quot;inside&quot;,&quot;inside&quot;,&quot;inside&quot;,
                         &quot;eating&quot;,&quot;eating&quot;,&quot;eating&quot;,&quot;eating&quot;,
                         &quot;sleeping&quot;,&quot;sleeping&quot;,&quot;sleeping&quot;,&quot;sleeping&quot;),
                     Value=c(1,1,0,0,1,0,0,1,0,0,1,1),
                     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;)),
                class= &quot;data.frame&quot;, row.names = c(NA,-12L))

Thank you

答案1

得分: 3

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

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

-output

# A tibble: 12 × 7
   A      B     C     D        H     Value Location
   <chr>  <chr> <chr> <chr>    <chr> <chr> <chr>   
 1 A_1_01 A     1     inside   1     1     I       
 2 A_1_01 A     1     inside   2     1     I       
 3 A_1_01 A     1     inside   3     0     O       
 4 A_1_01 A     1     inside   4     0     O       
 5 A_1_01 A     1     eating   1     1     I       
 6 A_1_01 A     1     eating   2     0     I       
 7 A_1_01 A     1     eating   3     0     O       
 8 A_1_01 A     1     eating   4     1     O       
 9 A_1_01 A     1     sleeping 1     0     I       
10 A_1_01 A     1     sleeping 2     0     I       
11 A_1_01 A     1     sleeping 3     1     O       
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

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

-output

# A tibble: 12 &#215; 7
   A      B     C     D        H     Value Location
   &lt;chr&gt;  &lt;chr&gt; &lt;chr&gt; &lt;chr&gt;    &lt;chr&gt; &lt;chr&gt; &lt;chr&gt;   
 1 A_1_01 A     1     inside   1     1     I       
 2 A_1_01 A     1     inside   2     1     I       
 3 A_1_01 A     1     inside   3     0     O       
 4 A_1_01 A     1     inside   4     0     O       
 5 A_1_01 A     1     eating   1     1     I       
 6 A_1_01 A     1     eating   2     0     I       
 7 A_1_01 A     1     eating   3     0     O       
 8 A_1_01 A     1     eating   4     1     O       
 9 A_1_01 A     1     sleeping 1     0     I       
10 A_1_01 A     1     sleeping 2     0     I       
11 A_1_01 A     1     sleeping 3     1     O       
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:

确定