将数据重塑为长格式。

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

Reshaping data into long forma

问题

I have reshaped the data as per your request:

  1. Name Value A B
  2. 1 ST01 1 0.020 0.010
  3. 2 ST01 2 0.040 0.020
  4. 3 ST01 3 0.050 0.040
  5. 4 ST01 4 0.060 0.080
  6. 5 ST01 5 0.080 0.090
  7. 6 ST02 1 0.036 0.003
  8. 7 ST02 2 0.034 0.002
  9. 8 ST02 3 0.054 0.008
  10. 9 ST02 4 0.034 0.009
  11. 10 ST02 5 0.240 0.004
  12. 11 ST03 1 0.140 0.040
  13. 12 ST03 2 0.320 0.020
  14. 13 ST03 3 0.560 0.060
  15. 14 ST03 4 0.780 0.080
  16. 15 ST03 5 0.590 0.050

This format should allow you to use ggplot to plot the data as you described. If you have any more questions or need further assistance, feel free to ask.

英文:

I have two large data frames, the sample below shows what they look like:

  1. Name<-c("ST01","ST02","ST03")
  2. B1<-c(0.01,0.003,0.04)
  3. B2<-c(0.02,0.002,0.02)
  4. B3<-c(0.04,0.008,0.06)
  5. B4<-c(0.08,0.009,0.08)
  6. B5<-c(0.09,0.004,0.05)
  7. df1<-data.frame(Name,B1,B2,B3,B4,B5)
  8. Name A1 A2 A3 A4 A5
  9. 1 ST01 0.020 0.040 0.050 0.060 0.08
  10. 2 ST02 0.036 0.034 0.054 0.034 0.24
  11. 3 ST03 0.140 0.320 0.560 0.780 0.59
  12. Name<-c("ST01","ST02","ST03")
  13. A1<-c(0.02,0.036,0.14)
  14. A2<-c(0.04,0.034,0.32)
  15. A3<-c(0.05,0.054,0.56)
  16. A4<-c(0.06,0.034,0.78)
  17. A5<-c(0.08,0.24,0.59)
  18. df2<-data.frame(Name,A1,A2,A3,A4,A5)
  19. Name B1 B2 B3 B4 B5
  20. 1 ST01 0.010 0.020 0.040 0.080 0.090
  21. 2 ST02 0.003 0.002 0.008 0.009 0.004
  22. 3 ST03 0.040 0.020 0.060 0.080 0.050

I would like to reshape them so that I can use ggplot to plot all the As against Bs and then plot As against Bs for each value, my desired output would be something like this:

  1. Name Value A B
  2. 1 ST01 1 0.020 0.010
  3. 2 ST01 2 0.040 0.020
  4. 3 ST01 3 0.050 0.040
  5. 4 ST01 4 0.060 0.080
  6. 5 ST01 5 0.080 0.090
  7. 6 STO2 1 0.036 0.003
  8. 7 STO2 2 0.034 0.002
  9. 8 STO2 3 0.054 0.008
  10. 9 STO2 4 0.034 0.009
  11. 10 STO2 5 0.240 0.004
  12. 11 ST03 1 0.140 0.040
  13. 12 ST03 2 0.320 0.020
  14. 13 ST03 3 0.560 0.060
  15. 14 ST03 4 0.780 0.080
  16. 15 ST03 5 0.590 0.050
  17. >

I am new to R so not quite if this can be done, or if there is a more appropriate way to shape the data in order to plot. Any help would be appreciated.

答案1

得分: 1

你可以首先使用 left_join 合并你的数据集,然后利用 pivot_longernames_pattern 参数和特殊的 ".value" 来将数据重塑为长格式。对于 names_pattern 参数,我使用正则表达式 "(.) (.)" 来将列名拆分为两个单字符字符串,例如,"A1" 被拆分为 "A" 和 "1",其中第一部分 ("A") 给出了值列的名称,第二部分 ("1") 存储在名称列 ("Value") 中作为类别:

英文:

You could first merge your datasets using a left_join then reshape to long by utilizing the names_pattern argument of pivot_longer and the special ".value". For the names_pattern argument I use the regex "(.)(.)" to split the column names into two one character strings, e.g. "A1" is split in to "A" and "1" where the first part ("A") gives the name of the value column and the second part ("1") is stored as category in the name column ("Value"):

  1. library(dplyr, warn = FALSE)
  2. library(tidyr)
  3. left_join(df2, df1, by = "Name") |>
  4. pivot_longer(-Name, names_to = c(".value", "Value"), names_pattern = "(.)(.)")
  5. #> # A tibble: 15 × 4
  6. #> Name Value A B
  7. #> <chr> <chr> <dbl> <dbl>
  8. #> 1 ST01 1 0.02 0.01
  9. #> 2 ST01 2 0.04 0.02
  10. #> 3 ST01 3 0.05 0.04
  11. #> 4 ST01 4 0.06 0.08
  12. #> 5 ST01 5 0.08 0.09
  13. #> 6 ST02 1 0.036 0.003
  14. #> 7 ST02 2 0.034 0.002
  15. #> 8 ST02 3 0.054 0.008
  16. #> 9 ST02 4 0.034 0.009
  17. #> 10 ST02 5 0.24 0.004
  18. #> 11 ST03 1 0.14 0.04
  19. #> 12 ST03 2 0.32 0.02
  20. #> 13 ST03 3 0.56 0.06
  21. #> 14 ST03 4 0.78 0.08
  22. #> 15 ST03 5 0.59 0.05

huangapple
  • 本文由 发表于 2023年6月15日 02:29:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/76476565.html
匿名

发表评论

匿名网友

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

确定