将TukeyHSD比较(第一列)分离成新的数据框架。将一列拆分为两列。

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

Separate out TukeyHSD comparisons (first column) into new data frame. Split one column into two

问题

I understand that you want to separate the first column of your data into two new columns in a data frame, where the values like 0.1-0 should be split into 0.1 and 0. Here's how you can do it in R:

  1. # Your original data
  2. data <- read.table(text = "
  3. diff lwr upr p adj
  4. 0.1-0 -7.6925867 -79.28269 63.89752 0.9999650
  5. 1-0 9.6882972 -40.93355 60.31014 0.9984060
  6. 10-0 4.1546350 -46.46721 54.77648 0.9999945
  7. 100-0 7.4767801 -44.70303 59.65659 0.9997580
  8. 1000-0 7.9958523 -41.34422 57.33593 0.9994555
  9. 10000-0 11.6970426 -42.41999 65.81407 0.9965756
  10. 100000-0 -104.3276885 -158.44472 -50.21066 0.0000106
  11. 1-0.1 17.3808839 -54.20922 88.97099 0.9930363
  12. 10-0.1 11.8472217 -59.74288 83.43732 0.9993756
  13. 100-0.1 15.1693668 -57.53073 87.86946 0.9972505
  14. 1000-0.1 15.6884389 -55.00112 86.37800 0.9959677
  15. 10000-0.1 19.3896293 -54.71317 93.49243 0.9891406
  16. 100000-0.1 -96.6351019 -170.73790 -22.53230 0.0038992
  17. 10-1 -5.5336622 -56.15551 45.08819 0.9999607
  18. 100-1 -2.2115171 -54.39132 49.96829 0.9999999
  19. 1000-1 -1.6924450 -51.03252 47.64763 1.0000000
  20. 10000-1 2.0087454 -52.10829 56.12578 1.0000000
  21. 100000-1 -114.0159857 -168.13302 -59.89895 0.0000019
  22. 100-10 3.3221451 -48.85766 55.50195 0.9999990
  23. 1000-10 3.8412172 -45.49886 53.18129 0.9999962
  24. 10000-10 7.5424076 -46.57462 61.65944 0.9997987
  25. 100000-10 -108.4823236 -162.59935 -54.36529 0.0000051
  26. 1000-100 0.5190721 -50.41818 51.45632 1.0000000
  27. 10000-100 4.2202625 -51.35684 59.79736 0.9999968
  28. 100000-100 -111.8044686 -167.38157 -56.22737 0.0000047
  29. 10000-1000 3.7011904 -49.21879 56.62117 0.9999982
  30. 100000-1000 -112.3235408 -165.24352 -59.40356 0.0000016
  31. 100000-10000 -116.0247312 -173.42451 -58.62495 0.0000043
  32. ", header = TRUE, row.names = 1, stringsAsFactors = FALSE)
  33. # Create a new data frame
  34. new_data <- data.frame(Group1 = as.numeric(sub('-.*', '', row.names(data))),
  35. Group2 = as.numeric(sub('.*-', '', row.names(data))),
  36. P = data$p.adj)
  37. # Print the new data frame
  38. print(new_data)

This code will create a new data frame new_data with the columns Group1, Group2, and P, where Group1 contains the values before the hyphen -, Group2 contains the values after the hyphen -, and P contains the p.adj values from your original data.

英文:

I am performing a TwoWay ANOVA on some data. When performing the TukeyHSD post-hoc, the following output is given:

  1. diff lwr upr p adj
  2. 0.1-0 -7.6925867 -79.28269 63.89752 0.9999650
  3. 1-0 9.6882972 -40.93355 60.31014 0.9984060
  4. 10-0 4.1546350 -46.46721 54.77648 0.9999945
  5. 100-0 7.4767801 -44.70303 59.65659 0.9997580
  6. 1000-0 7.9958523 -41.34422 57.33593 0.9994555
  7. 10000-0 11.6970426 -42.41999 65.81407 0.9965756
  8. 100000-0 -104.3276885 -158.44472 -50.21066 0.0000106
  9. 1-0.1 17.3808839 -54.20922 88.97099 0.9930363
  10. 10-0.1 11.8472217 -59.74288 83.43732 0.9993756
  11. 100-0.1 15.1693668 -57.53073 87.86946 0.9972505
  12. 1000-0.1 15.6884389 -55.00112 86.37800 0.9959677
  13. 10000-0.1 19.3896293 -54.71317 93.49243 0.9891406
  14. 100000-0.1 -96.6351019 -170.73790 -22.53230 0.0038992
  15. 10-1 -5.5336622 -56.15551 45.08819 0.9999607
  16. 100-1 -2.2115171 -54.39132 49.96829 0.9999999
  17. 1000-1 -1.6924450 -51.03252 47.64763 1.0000000
  18. 10000-1 2.0087454 -52.10829 56.12578 1.0000000
  19. 100000-1 -114.0159857 -168.13302 -59.89895 0.0000019
  20. 100-10 3.3221451 -48.85766 55.50195 0.9999990
  21. 1000-10 3.8412172 -45.49886 53.18129 0.9999962
  22. 10000-10 7.5424076 -46.57462 61.65944 0.9997987
  23. 100000-10 -108.4823236 -162.59935 -54.36529 0.0000051
  24. 1000-100 0.5190721 -50.41818 51.45632 1.0000000
  25. 10000-100 4.2202625 -51.35684 59.79736 0.9999968
  26. 100000-100 -111.8044686 -167.38157 -56.22737 0.0000047
  27. 10000-1000 3.7011904 -49.21879 56.62117 0.9999982
  28. 100000-1000 -112.3235408 -165.24352 -59.40356 0.0000016
  29. 100000-10000 -116.0247312 -173.42451 -58.62495 0.0000043

Is there an easy way to separate out the first column into a new data frame, (this is to be the data input for p_stat_manual in a ggplot, which requires the dataframe to be Group 1 | Group 2 | P))

i.e. column 1, row 1 says: 0.1-0. In the new dataframe how can I have 0.1 in column Group1 and 0 in column Group2 etc...

Currently I am writing by hand as follows, but this is time-consuming and is easy to introduce errors:

  1. group1 &lt;- (c(1, 10, 100, 1000, 10000, 100000, &quot;DMSO&quot;, 10, 100, 1000, 10000, 100000, &quot;DMSO&quot;, 100, 1000, 10000, 100000, &quot;DMSO&quot;, 1000, 10000, 100000, &quot;DMSO&quot;, 10000, 100000, &quot;DMSO&quot;, 100000, &quot;DMSO&quot;, &quot;DMSO&quot;))
  2. group2 &lt;- factor(c(0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 10, 10, 10, 10, 10, 100, 100, 100, 100, 1000, 1000,1000, 10000, 10000, 100000))

答案1

得分: 2

这里是如何执行的示例,我使用了 ?TukeyHSD 的示例。

  1. library(dplyr)
  2. library(tibble)
  3. library(tidyr)
  4. fm1 <- aov(breaks ~ wool + tension, data = warpbreaks)
  5. tukey <- TukeyHSD(fm1, "tension", ordered = TRUE)
  6. tukey$tension %>%
  7. as.data.frame() %>%
  8. rownames_to_column() %>%
  9. separate(col = rowname, into = c("var1", "var2"))
英文:

Here an example of how to do it, I used the ?TukeyHSD example.

  1. library(dplyr)
  2. library(tibble)
  3. library(tidyr)
  4. fm1 &lt;- aov(breaks ~ wool + tension, data = warpbreaks)
  5. tukey &lt;- TukeyHSD(fm1, &quot;tension&quot;, ordered = TRUE)
  6. tukey$tension %&gt;%
  7. as.data.frame() %&gt;%
  8. rownames_to_column() %&gt;%
  9. separate(col = rowname,into = c(&quot;var1&quot;,&quot;var2&quot;))

huangapple
  • 本文由 发表于 2023年1月9日 09:12:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/75052409.html
匿名

发表评论

匿名网友

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

确定