【发布时间】:2018-11-19 21:22:33
【问题描述】:
在我的数据集中,我有几个月的数据。
df=structure(list(id = c(1030879980L, 1030879990L), jan = c(170L,
265L), feb = c(153L, 332L), march = c(170L, 290L), apr = c(1L,
425L), may = c(66L, 406L), jume = c(125L, 352L), jul = c(129L,
339L), aug = c(-109L, 470L), sept = c(56L, 486L), oct = c(37L,
440L), nov = c(52L, 589L), dec = c(63L, 659L)), .Names = c("id",
"jan", "feb", "march", "apr", "may", "jume", "jul", "aug", "sept",
"oct", "nov", "dec"), class = "data.frame", row.names = c(NA,
-2L))
我必须为每个 id 执行一个样本学生的 t 检验 使用参考值
这里有参考值的数据
ref=structure(list(jan = 507L, feb = 502L, march = 431L, apr = 429L,
may = 449L, jume = 368L, jul = 406L, aug = 290L, sept = 309L,
oct = 371L, nov = 481L, dec = 536L), .Names = c("jan", "feb",
"march", "apr", "may", "jume", "jul", "aug", "sept", "oct", "nov",
"dec"), class = "data.frame", row.names = c(NA, -1L))
所以我很简单
#the first id 1030879980
a = c(170,153,170,1,66,125,129,-109,56,37,52,63)
#jan reference values for january
t.test (a, mu=507)
#feb reference values for febrary
t.test (a, mu=502)
但是我怎样才能按月为每个 id 执行呢? 当然,当我(手动)这样做时,它会很长。有很多id。
【问题讨论】:
-
也许只是
sapply(dat$month, function (x) t.test(a, mu=x))?你可能需要重塑或其他东西。或者sapply(dat, function (x) t.test(a, mu=x))如果您的 data.frame 是单行引用。 -
@lmo,我做了
-
sapply(dat, function (x) t.test(dat, mu=x)) Rerun with Debug Error in t.test.default(dat, mu = x) : 'mu' must be一个数字
-
数据集的结构是每个月都有单独的列,我可以在可重现的例子中看到它
-
x 必须是参考数据集中的参考值