Reputation: 102
I had table like this:
id BiotinControl1_2 BiotinControl2 BiotinControl3 BiotinTreatment1_2 BiotinTreatment2 BiotinTreatment3 Sequence
1 75 3893050.50 2717893.32 3206861.1 3435216.40 3768203.0 3647604.45 AAAAGAAAVANQGKK
2 192 900604.61 741299.33 937413.2 818936.89 937764.7 751303.46 AAAAGAAAVANQGKK
3 3770 90008.14 87127.07 107568.6 85120.95 101947.6 90152.82 AAFTKLDQVWGSE
I used the code below to reorganize my data:
tbl_reo <- melt(tbl_anv, measure.vars=2:7)
So now my data looks like below:
id Sequence variable value
1 75 AAAAGAAAVANQGKK BiotinControl1_2 3893050.50
2 192 AAAAGAAAVANQGKK BiotinControl1_2 900604.61
3 3770 AAFTKLDQVWGSE BiotinControl1_2 90008.14
I want to get an anova analysis. I want to do it id by id in a loop. So firstly I created a table for the id containing all 6 values and the variable column which states to which variable the value belongs to. I want to do a anova calculation for those 2 columns.
Edit: So for each id , I'd like to compute the lm(value~variable).
aov.test <- summary(aov(tbl_reo$value ~ as.factor(tbl_reo$variable)))
tbl_reo[,5] <- aov.test[[1]]$'Pr(>F)'[1]
Code which I used to calculate the anova but it's not working properly. I'd like to put the results in an extra column in my data.
tbl_anv <- tbl_all_onlyK[,c("id", "BiotinControl1_2", "BiotinControl2", "BiotinControl3", "BiotinTreatment1_2", "BiotinTreatment2", "BiotinTreatment3", "Sequence")]
tbl_reo <- melt(tbl_anv, measure.vars=2:7)
dat <- vector("integer", length = ncol(tbl_reo))
names(dat) <- colnames(tbl_reo)
for (var in variable) {
dat[var] <- anova(lm(value ~ tbl_reo[, var], data = tbl_reo))$"Pr(>F)"[1]
}
Upvotes: 0
Views: 2831
Reputation: 121608
Very hard to understand what do you want to do. Since you question is still not clear this is just an approximated answer....
But I think you are looking for by
or for ddply
from plyr
package, to do some treatment by group. For example, First I create some reproducible data. (Please learn how to reproduce data, it is essential to resolve your problem).
set.seed(1)
vars <- c("id", "BiotinControl1_2", "BiotinControl2", "BiotinControl3",
"BiotinTreatment1_2", "BiotinTreatment2", "BiotinTreatment3",
"Sequence")
tbl_reo <- data.frame(value = rnorm(100),
id = gl(6,100/6),
variable= sample(vars,100,rep=TRUE))
So my data is lokking like this :
str(tbl_reo)
'data.frame': 100 obs. of 3 variables:
$ value : num -0.626 0.184 -0.836 1.595 0.33 ...
$ id : Factor w/ 6 levels "1","2","3","4",..: 1 1 1 1 1 1 1 1 1 1 ...
$ variable: Factor w/ 8 levels "BiotinControl1_2",..: 2 1 4 2 1 4 4 1 2 5 ...
So for each id , I'd like to compute the lm(value~variable) and compare anova between ids...
So using by
you can get the anova for each id like this:
by(tbl_reo,tbl_reo$id,function(x){
anova(lm(value ~ variable, data = x))$"Pr(>F)"[1]
})
tbl_reo$id: 1
[1] 0.7430758
---------------------------------------------------------------------------------------------------------
tbl_reo$id: 2
[1] 0.122237
---------------------------------------------------------------------------------------------------------
tbl_reo$id: 3
[1] 0.8914668
---------------------------------------------------------------------------------------------------------
tbl_reo$id: 4
[1] 0.7790441
---------------------------------------------------------------------------------------------------------
tbl_reo$id: 5
[1] 0.6833726
---------------------------------------------------------------------------------------------------------
tbl_reo$id: 6
[1] 0.7323833
Upvotes: 3