开发者

Mean of One Column based of Level of other Column(s) in Loops

Take mean of last column (C5) of mf based on every factor (C1 to C4 in this case, if data files are different so no need to change anything, it should modify automatically because we would be using loops for assigning name and calculation.

the result will be like Mean of C1 when level is 1 is 5.5 Mean of C1 when level is 2 is 3.5 Mean of C2 when level is 1 is 6.5 Mean of C2 when level is 2 is 2.5 Mean of C3 when level is 1 is 4 Mean of C3 when level is 2 is 5

I want to be a general program, so when we change the data, no change would be in program, the sum will开发者_如何学运维 be based on the new data. No of factor and level could be change in new data.

# call excel file containing orthogonal profile
pf <- read.csv(file="pfile2.csv",head=FALSE,sep=",") 
# call excel file response / data 
rs <- read.csv(file="respf2.csv",head=FALSE,sep=",")
# Merge orthogonal arrays  and response in a single file
mf= merge(pf, rs, by="V1")
# Change the name of the new file/merge file all column contain word Cx
names(mf)<-sprintf("C%d",1:length(mf))
# total variable and total data 
tc=NCOL(mf)
tr=NROW(mf)
pf
  V1 V2 V3 V4
1  1  1  1  1
2  2  1  1  2
3  3  1  2  1
4  4  1  2  2
5  5  2  1  1
6  6  2  1  2
7  7  2  2  1
8  8  2  2  2

rs
   V1 V2
1  1  8
2  2  7
3  3  4
4  4  3
5  5  6
6  6  5
7  7  2
8  8  1

mf
   C1 C2 C3 C4 C5
1  1  1  1  1  8
2  2  1  1  2  7
3  3  1  2  1  4
4  4  1  2  2  3
5  5  2  1  1  6
6  6  2  1  2  5
7  7  2  2  1  2
8  8  2  2  2  1

tc
[1] 5
tr
[1] 8


Quite straight forward and can be done without loading any other packages.

aggregate(C5 ~ C2, mf, mean)
aggregate(C5 ~ C3, mf, mean)
aggregate(C5 ~ C4, mf, mean)

Should get you there. Didn't test as didn't have time to type your data in, see my comment on your question.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜