我查看了发布的问题/回复,但没有找到我要找的东西,我认为我并没有充分掌握R来自己获取它。
背景信息:我的数据集来自对动物生物标记物分析的实验(“animal1”,“animal2”,......,“animaln”),具有不同的处理方法(“a”,“b”或“c” “),每次治疗的不同重复(治疗”a“的”a1“,”a2“和”a3“等),以及不同的测量天数(”0“,”5“,”10“和”20“) 。在数据框中,我有一个变量“状态”,通知第x天动物的存活(= 1)或死亡(= 0)。
以下是结构类型的示例:
> str(data.set)
'data.frame': 108 obs. of 6 variables:
$ animal : Factor w/ 27 levels "animal1","animal10",..: 1 12 21 22 23 24 25 26 27 2 ...
$ treatment: Factor w/ 3 levels "a","b","c": 1 1 1 1 1 1 1 1 1 2 ...
$ replicate: Factor w/ 9 levels "a1","a2","a3",..: 1 1 1 2 2 2 3 3 3 4 ...
$ day : int 0 0 0 0 0 0 0 0 0 0 ...
$ status : int 1 1 1 1 1 1 1 1 1 1 ...
$ marker : logi NA NA NA NA NA NA ...
我想要一个循环,允许我自动计算特定日期每次治疗(和/或每次重复)的存活次数。
例如:在第20天,治疗“a”仍然有10只动物存活,而治疗“b”有8只,“c”有5只。我希望我的解释清楚。我试图自己找到一个循环,但实际上我没有得到任何东西......我认为这样的循环对于许多生物学家来说非常有用,他们是R命令的初学者......
非常感谢,
答案 0 :(得分:0)
这是输出的问题:
dput(head(data.set))
structure(list(animal = structure(c(1L, 12L, 21L, 22L, 23L, 24L
), .Label = c("animal1", "animal10", "animal11", "animal12",
"animal13", "animal14", "animal15", "animal16", "animal17", "animal18",
"animal19", "animal2", "animal20", "animal21", "animal22", "animal23",
"animal24", "animal25", "animal26", "animal27", "animal3", "animal4",
"animal5", "animal6", "animal7", "animal8", "animal9"), class = "factor"),
treatment = structure(c(1L, 1L, 1L, 1L, 1L, 1L), .Label = c("a",
"b", "c"), class = "factor"), replicate = structure(c(1L,
1L, 1L, 2L, 2L, 2L), .Label = c("a1", "a2", "a3", "b1", "b2",
"b3", "c1", "c2", "c3"), class = "factor"), day = c(0L, 0L,
0L, 0L, 0L, 0L), status = c(1L, 1L, 1L, 1L, 1L, 1L), marker = c(NA,
NA, NA, NA, NA, NA)), .Names = c("animal", "treatment", "replicate",
"day", "status", "marker"), row.names = c(NA, 6L), class = "data.frame")
答案 1 :(得分:0)
我终于找到了这个命令,实际上这很简单,但之前我不知道聚合函数,所以在我的情况下不需要创建一个循环......:
聚合(status~group + day,data = data.set,sum)
这是有效的!谢谢你alexis_laz