dcast函數的問題(reshape2) - 三個變量組合

[英]Issues with dcast function (reshape2) - three variable combination

I am using reshape2 package to shape my data and use it for t-test. For me it is easier to visualize the data in separate columns. I have three treatment combinations where "wat" is nested within "spp" and "ins" is nested within water. My demo table contains 3 response variable namely "tyr", "esc" and "esc_R". I would be interested in seeing how ins influence response -> "tyr" in "spp" -> Bl, with treatment "wat" -> High (just an example).

我正在使用reshape2包來塑造我的數據並將其用於t檢驗。對我來說,在單獨的列中可視化數據更容易。我有三種治療組合,其中“wat”嵌套在“spp”中,“ins”嵌套在水中。我的演示表包含3個響應變量,即“tyr”,“esc”和“esc_R”。我有興趣看看ins如何影響響​​應 - >“tyr”在“spp” - > Bl中,處理“wat” - > High(只是一個例子)。

Here is my data: demo.data


## Use orderBy function to sort data
demo <- orderBy(~spp+wat+ins, data = demo)
## Create an unique data frame for a specific variable
df.bl.ins.1 <- demo[demo$spp == "Bl", c(1:3, 4)]
df.bl.ins.2 <- df.bl.ins.1[df.bl.ins.1$wat == "High", ]

And then I am having trouble executing dcast function.


df.bl.ins.tmp <- dcast(df.bl.ins.2, spp + wat ~ ins, value.var = "tyr")

I have found interesting information in the following threads


  1. Dason's suggestion - which works really well with ToothGrowth demo dataset. Unfortunately, when the table has multiple treatments (more than 2) the solution did not remain simple. I agree with Maiasaura's suggestions that creating an unique variable is the key to this problem. However, I am having hard time understanding what function(x) does or how to use it in my table.
  2. Dason的建議 - 與ToothGrowth演示數據集配合得非常好。不幸的是,當桌子有多次治療(超過2次)時,解決方案並不簡單。我同意Maiasaura的建議,即創建一個獨特的變量是這個問題的關鍵。但是,我很難理解函數(x)在我的表中做了什么或如何使用它。

Any help in this regard is much appreciated.


In addition, if you have alternative suggestions to do t-test without manipulating the original data frame (demo), I will be excited to hear about it.


Thanks in advance.


Edit Here is what I am expecting, for "tyr". In the following format I desire to compare "No" vs. "Yes" using a t-test.


spp wat ins No  Yes
Bl  High    No  0.3036  0.1987
Bl  High    No  0.2577  0.1112
Bl  High    No  NA  0.199
Bl  High    No  0.3299  0.1886
Bl  High    No  0.3301  0.2332

1 个解决方案



Perhaps I don't understand exactly what you want to do, but I think you could run linear regression directly on your data. In this way, you could do t-tests on whether the coefficients of your model were zero or not. I think this might suffice, and serve also to tease apart the effects of each of your independent variables. Here is an example:


             Estimate Std. Error t value Pr(>|t|)    
(Intercept)  0.286386   0.016500  17.356  < 2e-16 ***
sppMan      -0.159514   0.015811 -10.089  1.3e-11 ***
watLow      -0.005501   0.015858  -0.347 0.730861    
insYes      -0.066741   0.015858  -4.209 0.000185 ***

This will get you a t test for just the groups that you showed in your example:


t.test(tyr~ins,data=df[df$spp=='Bl' & df$wat=='High',])



粤ICP备14056181号  © 2014-2021 ITdaan.com