ggadjustedcurves error: Must use a vector in '[', not an object of class matrix
Asked Answered
S

1

8

ggadjustedcurves error: Must use a vector in '[', not an object of class matrix

I call rlang::last_trace() and get this:

> 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)

Trying to plot adjusted curves using a strata variable.

Sender answered 20/3, 2019 at 22:16 Comment(2)
Can you include your data using dput and the code you were running that resulted in the error so we can reproduce it?Flatten
The error message is rather strange. The [ function is generally a perfectly acceptable function to use on a matrix, but it is "generic". With the tidyverse it's difficult to know sometimes what class objects are throwing errors. You should definitely post a minimal reproducible example.Shakeup
P
10

I have a very similar problem but when I generated a sample data set to post it the plots work which makes me think it is my data that is an issue. Here is the reproducible example in which both plots are generated nicely.

# 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)

When I run this code on my data the first plot works nicely but the faceted plot returns this error

> 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)

The column names used in the sample data are identical to those in my dataset and the data types are the same i.e. all columns except Survival and Censored are factors.

EDIT

I've fixed it, having decided that it was definitely a data issue I looked at the structure of the sample data and my data.

> str(x1502)
Classes ‘tbl_df’, ‘tbl’ and 'data.frame':   450 obs. of  7 variables:

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

So I used x1502 <- as.data.frame(x1502) to convert my data to a data frame and now it all works nicely. Hopefully this will work for your data mindhabits.

Providenciaprovident answered 22/3, 2019 at 11:46 Comment(3)
@C Jones Thank you for your comments. It worked wonderfully. I converted the data to a dataframe in the manner you described and had it run successfully.Sender
I obtained a similar error trying to facet survival curves where the input data was a 'tbl_df', 'tbl'. Running as.data.frame on it solved the problem as well.Showers
I had already tried data frame conversion before I found this question / answer, and as such was disappointed that the fix worked for others. Then, I remembered there was two objects being acted on in my code, and I assumed that one was already a data frame. I converted the other object and it ran successfully :)Deneb

© 2022 - 2024 — McMap. All rights reserved.