将列中的数据展开为多行

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

expand data from column to multiple rows

问题

这是我试图在R中扩展到多列的数据的一个示例练习

  1. Location <- c(rep("ATL", 3), rep("CLE", 3), rep("BOS", 3))
  2. type <- rep(c("sedan", "truck", "coupe"), 3)
  3. pro1 <- rep(c("0800 SC", "1500 SC", "0900 SC"), 3)
  4. pro2 <- rep(c("0800 SC", "1500 SC", "0900 SC"), 3)
  5. pro3 <- rep(c("1000 SC", "1700 SC", "1100 SC"), 3)
  6. pro4 <- rep(c("1300 SC", "1100 SC", "2300 SC"), 3)
  7. x <- data.frame(Location, type, pro1, pro2, pro3, pro4)

我试图将数据转移到Excel文件的屏幕截图

期望的输出(来自Excel的屏幕截图):
将列中的数据展开为多行

当前的输出:
将列中的数据展开为多行

英文:

This is a practice an example of the data I am trying to expand over multiple columns in R

  1. Location&lt;-c(rep(&quot;ATL&quot;,3),rep(&quot;CLE&quot;,3),rep(&quot;BOS&quot;,3))
  2. type&lt;-rep(c(&quot;sedan&quot;,&quot;truck&quot;,&quot;coupe&quot;),3)
  3. pro1&lt;-rep(c(&quot;0800 SC&quot;,&quot;1500 SC&quot;,&quot;0900 SC&quot;),3)
  4. pro2&lt;- rep(c(&quot;0800 SC&quot;,&quot;1500 SC&quot;,&quot;0900 SC&quot;),3)
  5. pro3&lt;-rep(c(&quot;1000 SC&quot;,&quot;1700 SC&quot;,&quot;1100 SC&quot;),3)
  6. pro4&lt;-rep(c(&quot;1300 SC&quot;,&quot;1100 SC&quot;,&quot;2300 SC&quot;),3)
  7. x&lt;-data.frame(Location,type,pro1,pro2,pro3,pro4)

I am trying to transfer the data into the screenshot of the excel file

Desired output (screenshot from Excel):
将列中的数据展开为多行

Current output:
将列中的数据展开为多行

答案1

得分: 0

你只需要使用 tidyr::pivot_longer()

  1. library(tidyr)
  2. Location<-c(rep("ATL",3),rep("CLE",3),rep("BOS",3))
  3. type<-rep(c("sedan","truck","coupe"),3)
  4. pro1<-rep(c("0800 SC","1500 SC","0900 SC"),3)
  5. pro2<- rep(c("0800 SC","1500 SC","0900 SC"),3)
  6. pro3<-rep(c("1000 SC","1700 SC","1100 SC"),3)
  7. pro4<-rep(c("1300 SC","1100 SC","2300 SC"),3)
  8. x<-data.frame(Location,type,pro1,pro2,pro3,pro4)
  9. x |> pivot_longer(starts_with("pro"), names_to = "pro", values_to = "time")
  10. #> # A tibble: 36 × 4
  11. #> Location type pro time
  12. #> <chr> <chr> <chr> <chr>
  13. #> 1 ATL sedan pro1 0800 SC
  14. #> 2 ATL sedan pro2 0800 SC
  15. #> 3 ATL sedan pro3 1000 SC
  16. #> 4 ATL sedan pro4 1300 SC
  17. #> 5 ATL truck pro1 1500 SC
  18. #> 6 ATL truck pro2 1500 SC
  19. #> 7 ATL truck pro3 1700 SC
  20. #> 8 ATL truck pro4 1100 SC
  21. #> 9 ATL coupe pro1 0900 SC
  22. #> 10 ATL coupe pro2 0900 SC
  23. #> # ℹ 26 more rows

创建于2023-07-05,使用 reprex v2.0.2

英文:

You just need to use tidyr::pivot_longer().

  1. library(tidyr)
  2. Location&lt;-c(rep(&quot;ATL&quot;,3),rep(&quot;CLE&quot;,3),rep(&quot;BOS&quot;,3))
  3. type&lt;-rep(c(&quot;sedan&quot;,&quot;truck&quot;,&quot;coupe&quot;),3)
  4. pro1&lt;-rep(c(&quot;0800 SC&quot;,&quot;1500 SC&quot;,&quot;0900 SC&quot;),3)
  5. pro2&lt;- rep(c(&quot;0800 SC&quot;,&quot;1500 SC&quot;,&quot;0900 SC&quot;),3)
  6. pro3&lt;-rep(c(&quot;1000 SC&quot;,&quot;1700 SC&quot;,&quot;1100 SC&quot;),3)
  7. pro4&lt;-rep(c(&quot;1300 SC&quot;,&quot;1100 SC&quot;,&quot;2300 SC&quot;),3)
  8. x&lt;-data.frame(Location,type,pro1,pro2,pro3,pro4)
  9. x |&gt; pivot_longer(starts_with(&quot;pro&quot;), names_to = &quot;pro&quot;, values_to = &quot;time&quot;)
  10. #&gt; # A tibble: 36 &#215; 4
  11. #&gt; Location type pro time
  12. #&gt; &lt;chr&gt; &lt;chr&gt; &lt;chr&gt; &lt;chr&gt;
  13. #&gt; 1 ATL sedan pro1 0800 SC
  14. #&gt; 2 ATL sedan pro2 0800 SC
  15. #&gt; 3 ATL sedan pro3 1000 SC
  16. #&gt; 4 ATL sedan pro4 1300 SC
  17. #&gt; 5 ATL truck pro1 1500 SC
  18. #&gt; 6 ATL truck pro2 1500 SC
  19. #&gt; 7 ATL truck pro3 1700 SC
  20. #&gt; 8 ATL truck pro4 1100 SC
  21. #&gt; 9 ATL coupe pro1 0900 SC
  22. #&gt; 10 ATL coupe pro2 0900 SC
  23. #&gt; # ℹ 26 more rows

<sup>Created on 2023-07-05 with reprex v2.0.2</sup>

huangapple
  • 本文由 发表于 2023年7月6日 10:38:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/76625162.html
匿名

发表评论

匿名网友

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

确定