【发布时间】:2017-11-02 19:07:22
【问题描述】:
dput(head(data,20))的输出
structure(list(Id = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), time = 1:20, Event = c(0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L), Fup = c(90L, 90L, 90L, 90L, 90L, 90L, 90L, 90L,
90L, 90L, 90L, 90L, 90L, 90L, 90L, 90L, 90L, 90L, 90L, 90L),
Start = 1:20, Stop = 2:21, dose = c(0, 0, 2.6, 2.6, 2.6,
2.6, 2.6, 2.6, 2.6, 2.6, 2.6, 2.6, 2.6, 2.6, 2.6, 2.6, 2.6,
2.6, 2.6, 2.6)), .Names = c("Id", "time", "Event", "Fup",
"Start", "Stop", "dose"), row.names = c(NA, 20L), class = "data.frame")
我需要在我的数据集中创建一个变量,名为:药物使用的总持续时间(在给定患者的整个随访期间),我正在创建以下循环:
list.id <- unique(data$Id)
nbr.subjects <- length(list.id)
tot.dur.use <- NULL
for (i in 1:nbr.subjects) {
current.subject <- data[data$Id == list.id[i], ]
tot.dur.use.tmp <- sum(current.subject$dose != 0)
tot.dur.use <- c(tot.dur.use, tot.dur.use.tmp)
}
data <- cbind(data, tot.dur.use)
给出以下错误:**data.frame(..., check.names = FALSE) 中的错误: 参数意味着不同的行数:70255、498 ** 有谁知道我在这里做错了什么? 谢谢!!!
【问题讨论】:
-
请提供数据集
data,或者如果它非常大,请提供它的样本。使用dput(data)并在您的问题中发布结果。 -
做
tot.dur.use <- c(tot.dur.use, tot.dur.use.tmp),而不是tot.dur.use.tmp <- c(...)。 -
数据集相当大,但这里是 head(data) 的结果: Id time Event Fup Start Stop dose 1 1 1 0 90 1 2 0.0 2 1 2 0 90 2 3 0.0 3 1 3 0 90 3 4 2.6 4 1 4 0 90 4 5 2.6 5 1 5 0 90 5 6 2.6 6 1 6 0 90 6 7 2.6
-
@RuiBarradas,我试着希望循环:tot.dur.use.tmp
-
抱歉,
suminstruction 是新的,它在您的代码中的什么位置?使用该更改和head(data)的输出编辑问题,不要将其放在评论中。发布dput(head(data, 20))的输出比简单的head(...)更好。
标签: r