gpt4 book ai didi

r - ggadjustedcurves 错误 : Must use a vector in '[' , 不是类矩阵的对象

转载 作者:行者123 更新时间:2023-12-03 14:59:22 26 4
gpt4 key购买 nike

ggadjustedcurves 错误:必须在“[”中使用向量,而不是类矩阵的对象

我打电话rlang::last_trace()得到这个:

> rlang::last_error()
<error>
message: Must use a vector in `[`, not an object of class matrix.
class: `rlang_error`
backtrace:
1. survminer::ggadjustedcurves(...)
2. survminer:::ggadjustedcurves.average(data, fit, variable, size = size)
4. base::sort.default(unique(data[, variable]))
8. base::order(x, na.last = na.last, decreasing = decreasing)
9. base::lapply(z, function(x) if (is.object(x)) as.vector(xtfrm(x)) else x)
10. base:::FUN(X[[i]], ...)
13. base::xtfrm.default(x)
15. base::rank(x, ties.method = "min", na.last = "keep")
17. tibble:::`[.tbl_df`(x, !nas)
18. tibble:::check_names_df(i, x)

尝试使用分层变量绘制调整后的曲线。

最佳答案

我有一个非常相似的问题,但是当我生成一个样本数据集来发布它时,绘图工作让我认为这是我的数据有问题。这是可重现的示例,其中两个图都很好地生成。

# generate reproducible data set
set.seed(3)
sampleData <- data.frame(Has_an_A_allele = sample(c("Yes", "No"), 1000, replace = TRUE),
Survival = rexp(1000, 0.5),
Censored = as.numeric(sample(c("1", "0"), 1000, replace = TRUE)),
Disease = sample(c("A", "B"), 1000, replace = TRUE),
Gender = sample(c("Male", "Female"), 1000, replace = TRUE),
Stage = sample(c("Early", "Advanced"), 1000, replace = TRUE),
Age = sample(c("Under 60", "Over 60"), 1000, replace = TRUE))
Summary(sampleData)

# create survival fit
fit<-survfit(Surv(Survival, Censored) ~ Has_an_A_allele, data = sampleData)

# create survival plot with p value
ggsurvplot(fit5, sampleData, xlim = c(0, 10), break.time.by = 2, pval = TRUE)

# create faceted survival plot with p value
ggsurvplot(fit5, sampleData, xlim = c(0, 10), break.time.by = 2, facet.by = "Disease", pval = TRUE)

当我在我的数据上运行此代码时,第一个图运行良好,但分面图返回此错误
> ggsurvplot(fit4, x1502, xlim = c(0, 10), break.time.by = 2, facet.by = "Gender", pval = TRUE)
Error: Must use a vector in `[`, not an object of class matrix.
Call `rlang::last_error()` to see a backtrace
> rlang::last_error()
<error>
message: Must use a vector in `[`, not an object of class matrix.
class: `rlang_error`
backtrace:
1. survminer::ggsurvplot(...)
4. survminer::surv_group_by(data, grouping.vars = facet.by)
5. survminer:::.levels(data[, grouping.vars])
6. base::as.factor(x)
7. base::factor(x)
8. base::order(y)
9. base::lapply(z, function(x) if (is.object(x)) as.vector(xtfrm(x)) else x)
10. base:::FUN(X[[i]], ...)
13. base::xtfrm.default(x)
15. base::rank(x, ties.method = "min", na.last = "keep")
17. tibble:::`[.tbl_df`(x, !nas)
18. tibble:::check_names_df(i, x)


示例数据中使用的列名与我的数据集中的列名相同,数据类型相同,即除 Survival 和 Censored 之外的所有列都是因子。

编辑

我已经修复了它,确定这绝对是一个数据问题,我查看了示例数据和我的数据的结构。
> str(x1502)
Classes ‘tbl_df’, ‘tbl’ and 'data.frame': 450 obs. of 7 variables:

> str(sampleData)
'data.frame': 1000 obs. of 7 variables:


所以我用了 x1502 <- as.data.frame(x1502)将我的数据转换为数据框,现在一切正常。希望这对您的数据思维习惯有用。

关于r - ggadjustedcurves 错误 : Must use a vector in '[' , 不是类矩阵的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55270964/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com