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

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

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:

# Your original data
data <- read.table(text = "
                   diff        lwr       upr     p adj
0.1-0          -7.6925867  -79.28269  63.89752 0.9999650
1-0             9.6882972  -40.93355  60.31014 0.9984060
10-0            4.1546350  -46.46721  54.77648 0.9999945
100-0           7.4767801  -44.70303  59.65659 0.9997580
1000-0          7.9958523  -41.34422  57.33593 0.9994555
10000-0        11.6970426  -42.41999  65.81407 0.9965756
100000-0     -104.3276885 -158.44472 -50.21066 0.0000106
1-0.1          17.3808839  -54.20922  88.97099 0.9930363
10-0.1         11.8472217  -59.74288  83.43732 0.9993756
100-0.1        15.1693668  -57.53073  87.86946 0.9972505
1000-0.1       15.6884389  -55.00112  86.37800 0.9959677
10000-0.1      19.3896293  -54.71317  93.49243 0.9891406
100000-0.1    -96.6351019 -170.73790 -22.53230 0.0038992
10-1           -5.5336622  -56.15551  45.08819 0.9999607
100-1          -2.2115171  -54.39132  49.96829 0.9999999
1000-1         -1.6924450  -51.03252  47.64763 1.0000000
10000-1         2.0087454  -52.10829  56.12578 1.0000000
100000-1     -114.0159857 -168.13302 -59.89895 0.0000019
100-10          3.3221451  -48.85766  55.50195 0.9999990
1000-10         3.8412172  -45.49886  53.18129 0.9999962
10000-10        7.5424076  -46.57462  61.65944 0.9997987
100000-10    -108.4823236 -162.59935 -54.36529 0.0000051
1000-100        0.5190721  -50.41818  51.45632 1.0000000
10000-100       4.2202625  -51.35684  59.79736 0.9999968
100000-100   -111.8044686 -167.38157 -56.22737 0.0000047
10000-1000      3.7011904  -49.21879  56.62117 0.9999982
100000-1000  -112.3235408 -165.24352 -59.40356 0.0000016
100000-10000 -116.0247312 -173.42451 -58.62495 0.0000043
", header = TRUE, row.names = 1, stringsAsFactors = FALSE)

# Create a new data frame
new_data <- data.frame(Group1 = as.numeric(sub('-.*', '', row.names(data))),
                       Group2 = as.numeric(sub('.*-', '', row.names(data))),
                       P = data$p.adj)

# Print the new data frame
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:

                   diff        lwr       upr     p adj
0.1-0          -7.6925867  -79.28269  63.89752 0.9999650
1-0             9.6882972  -40.93355  60.31014 0.9984060
10-0            4.1546350  -46.46721  54.77648 0.9999945
100-0           7.4767801  -44.70303  59.65659 0.9997580
1000-0          7.9958523  -41.34422  57.33593 0.9994555
10000-0        11.6970426  -42.41999  65.81407 0.9965756
100000-0     -104.3276885 -158.44472 -50.21066 0.0000106
1-0.1          17.3808839  -54.20922  88.97099 0.9930363
10-0.1         11.8472217  -59.74288  83.43732 0.9993756
100-0.1        15.1693668  -57.53073  87.86946 0.9972505
1000-0.1       15.6884389  -55.00112  86.37800 0.9959677
10000-0.1      19.3896293  -54.71317  93.49243 0.9891406
100000-0.1    -96.6351019 -170.73790 -22.53230 0.0038992
10-1           -5.5336622  -56.15551  45.08819 0.9999607
100-1          -2.2115171  -54.39132  49.96829 0.9999999
1000-1         -1.6924450  -51.03252  47.64763 1.0000000
10000-1         2.0087454  -52.10829  56.12578 1.0000000
100000-1     -114.0159857 -168.13302 -59.89895 0.0000019
100-10          3.3221451  -48.85766  55.50195 0.9999990
1000-10         3.8412172  -45.49886  53.18129 0.9999962
10000-10        7.5424076  -46.57462  61.65944 0.9997987
100000-10    -108.4823236 -162.59935 -54.36529 0.0000051
1000-100        0.5190721  -50.41818  51.45632 1.0000000
10000-100       4.2202625  -51.35684  59.79736 0.9999968
100000-100   -111.8044686 -167.38157 -56.22737 0.0000047
10000-1000      3.7011904  -49.21879  56.62117 0.9999982
100000-1000  -112.3235408 -165.24352 -59.40356 0.0000016
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:

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;))
 
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 的示例。

library(dplyr)
library(tibble)
library(tidyr)

fm1 <- aov(breaks ~ wool + tension, data = warpbreaks)

tukey <- TukeyHSD(fm1, "tension", ordered = TRUE)

tukey$tension %>%
  as.data.frame() %>%
  rownames_to_column() %>%
  separate(col = rowname, into = c("var1", "var2"))
英文:

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

library(dplyr)
library(tibble)
library(tidyr)

fm1 &lt;- aov(breaks ~ wool + tension, data = warpbreaks)

tukey &lt;- TukeyHSD(fm1, &quot;tension&quot;, ordered = TRUE)

tukey$tension %&gt;% 
  as.data.frame() %&gt;% 
  rownames_to_column() %&gt;% 
  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:

确定