【发布时间】:2012-07-30 19:53:52
【问题描述】:
所以我的测试数据是这样的:
structure(list(day = c(1L, 1L, 2L, 2L, 2L, 3L, 3L, 4L, 4L, 4L
), Left = c(0.25, 0.33, 0, 0, 0.25, 0.33, 0.5, 0.33, 0.5, 0),
Left1 = c(NA, NA, 0, 0.5, 0.25, 0.33, 0.1, 0.33, 0.5, 0),
Middle = c(0, 0, 0.3, 0, 0.25, 0, 0.3, 0.33, 0, 0), Right = c(0.25,
0.33, 0.3, 0.5, 0.25, 0.33, 0.1, 0, 0, 0.25), Right1 = c(0.5,
0.33, 0.3, 0, 0, 0, 0, 0, 0, 0.75), Side = structure(c(2L,
2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 2L), .Label = c("L", "R"), class = "factor")), .Names = c("day",
"Left", "Left1", "Middle", "Right", "Right1", "Side"), class = "data.frame", row.names = c(NA,
-10L))
或者这个:
day Left Left1 Middle Right Right1 Side
1 0.25 NA 0.00 0.25 0.50 R
1 0.33 NA 0.00 0.33 0.33 R
2 0.00 0.00 0.30 0.30 0.30 R
2 0.00 0.50 0.00 0.50 0.00 R
2 0.25 0.25 0.25 0.25 0.00 L
3 0.33 0.33 0.00 0.33 0.00 L
我想写一个循环来查找所选一侧的每一天的标准误差和平均值..
好的..到目前为止我有这个代码:
td<-read.csv('test data.csv')
IDs<-unique(td$day)
se<-function(x) sqrt(var(x)/length(x))
for (i in 1:length (IDs)) {
day.i<-which(td$day==IDs[i])
td.i<-td[day.i,]
if(td$Side=='L'){
side<-cbind(td.i$Left + td.i$Left1)
}else{
side<-cbind(td.i$Right + td.i$Right1)
}
mean(side)
se(side)
print(mean)
print(se)
}
但我收到这样的错误消息
错误:“}”中出现意外的“}”
显然,我也没有每天都无法打印出来。有谁知道为什么?
也在这里工作:http://www.talkstats.com/showthread.php/27187-Writing-a-mean-loop..-(literally)
【问题讨论】:
-
要明确一点:在真实数据中,每天会有多行吗?
-
是的..每天都有几行
-
如果没有偏好怎么办?例如任何一天都有两个“R”和两个“L”
-
每天有几行条目..每一行都是独立的...例如..如果在第2天有两个选择右和一个左..我想找到三者的平均值..我将为'R'侧绑定right和right1,我将为'L'侧绑定Left和Left1并找到这三个的平均值......然后我会有一个平均值每天在所选方面花费的价值.. 这有意义吗?