我有一個數據幀看起來像這樣(使用reshape2 ::演員和合並):R:如何根據多列上的因子水平來減去值?
time days treatment extrafactor1 extrafactor2 extrafactor3 animal1 animal2 animal3
10 83 control B water 2 2 67 40
10 83 control B water 3 50 67 39
10 83 control A water 3 22 80 63
10 83 control A water 2 40 40 100
10 83 treated A water 3 40 69 92
10 83 treated A water 1 64 56 6
10 83 treated A water 2 90 67 52
10 83 treated B water 2 14 36 77
10 83 treated B water 3 41 83 55
10 83 treated B water 1 66 31 51
11 86 control B water 1 99 100 10
11 86 control B water 2 23 27 22
11 86 control A water 3 57 10 65
11 86 control A water 1 60 2 49
11 86 control A water 2 23 14 44
11 86 control B water 3 97 45 20
11 86 treated B water 2 71 15 24
11 86 treated B water 3 49 55 63
11 86 treated A water 3 54 88 27
,我想從處理樣品的。減去不同的動物的對照樣品的數值。當然,減數應該在其他因素的水平匹配的地方進行,所以「11_86_reatment_A_water_3」的動物1值應該被「11_86_control_A_water_3」的動物1值所減少,並且這對於每隻動物都是這樣。我一直想一些事情plyr像
df2 <- ddply(df, .(time,days,treatment,extrafactor1,extrafactor2,extrafactor3), transform, animal1 = animal1-animal1[treatment=="control"])
,但它給了我很多的NAS和我敢肯定有信息遺漏充分執行,我想有什麼。實際上有100多種動物。
我嘗試就是從這裏適應,但也有少輸入變量,少列執行上:Easiest way to subtract associated with one factor level from values associated with all other factor levels 這裏:R ddply with multiple variables
這也有可能等到重塑表到長格式對於ggplot,如果這使事情變得更容易?
對我有什麼建議嗎?
嘿!看看https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example來創建一個可重現的例子。這使得它很容易幫助。 – elevendollar
你的預期輸出是什麼 – Wen