我大家!For循环和功能错误与R
我发现了我的博士研究生的世界,当我想要实现循环来简化分析时,我遇到了一些问题。
我的数据帧是:
'data.frame': 3581 obs. of 8 variables:
$ Date : Factor w/ 7 levels "03-03-17","10-02-17",..:
$ Experimentator: Factor w/ 9 levels "BURLET","DECHAUD",..:
$ Origin : Factor w/ 3 levels "FRANCE","JAPAN",..:
$ City : Factor w/ 6 levels "MONTPELLIER",..:
$ Lineage : Factor w/ 27 levels "L21","L22","L26",..:
$ Sex : Factor w/ 2 levels "Female","Male":
$ ccr : int 1183 1813 1866 1745 1210 1463 2477 1506
前6是我的因素和最后我的定量变量。 我需要在同一时间几个因素的工作,然后当我想做例如shapiro.test: 由:
by(data$ccr, c(data$Date, data$Sex, data$Lineage), shapiro.test())
Error in tapply(seq_len(3581L), list(`c(data$Date, data$Sex,
data$Lineage)` = c(2L, : the arguments must have the same length
有了一个for循环很难给我,让我试着写:
for(sex in levels(data$Sex)){
for(date in levels(data$Date)){
for(lineage in levels(data$Lineage)){
shapiro.test(data$ccr[,lineage])
}
}
}
BU我不知道如何来增加我的环...
感谢您的帮助!
接收数据你至少应该告诉我们你在使用它在这里封装(S)。 –
您需要所有3581条记录? –
在'by'中,分组变量('INDICES')应该是“一个_'列表因素”。比较'(mtcars,c(mtcars $ vs,mtcars $ am),function(dat)shapiro.test(dat $ mpg))''(您的情况),'by(mtcars,list(mtcars $ vs,mtcars $ am),函数(dat)shapiro.test(dat $ mpg))'。 – Henrik