简体   繁体   中英

Fixing Error in eval(m$data, parent.frame()) : object 'dzclass' not found in R

I have this data frame (reproducible):

structure(list(age = c(62.84998, 60.33899, 52.74698, 42.38498
 ), death = c(0, 1, 1, 1), sex = c("male", "female", "female", 
 "female"), hospdead = c(0, 1, 0, 0), slos = c(5, 4, 17, 3), d.time = c(2029, 
 4, 47, 133), dzgroup = c("Lung Cancer", "Cirrhosis", "Cirrhosis", 
 "Lung Cancer"), dzclass = c("Cancer", "COPD/CHF/Cirrhosis", "COPD/CHF/Cirrhosis", 
 "Cancer"), num.co = c(0, 2, 2, 2), edu = c(11, 12, 12, 11), income = c("$11-$25k", 
 "$11-$25k", "under $11k", "under $11k"), scoma = c(0, 44, 0, 
 0), charges = c(9715, 34496, 41094, 3075), totcst = c(NA_real_, 
 NA_real_, NA_real_, NA_real_), totmcst = c(NA_real_, NA_real_, 
 NA_real_, NA_real_), avtisst = c(7, 29, 13, 7), race = c("other", 
 "white", "white", "white"), sps = c(33.8984375, 52.6953125, 20.5, 
 20.0976562), aps = c(20, 74, 45, 19), surv2m = c(0.262939453, 
 0.0009999275, 0.790893555, 0.698974609), surv6m = c(0.0369949341, 
 0, 0.664916992, 0.411987305), hday = c(1, 3, 4, 1), diabetes = c(0, 
 0, 0, 0), dementia = c(0, 0, 0, 0), ca = c("metastatic", "no", 
 "no", "metastatic"), prg2m = c(0.5, 0, 0.75, 0.899999619), prg6m = c(0.25, 
 0, 0.5, 0.5), dnr = c("no dnr", NA, "no dnr", "no dnr"), dnrday = c(5, 
 NA, 17, 3), meanbp = c(97, 43, 70, 75), wblc = c(6, 17.0976562, 
 8.5, 9.09960938), hrt = c(69, 112, 88, 88), resp = c(22, 34, 
 28, 32), temp = c(36, 34.59375, 37.39844, 35), pafi = c(388, 
 98, 231.65625, NA), alb = c(1.7998047, NA, NA, NA), bili = c(0.19998169, 
 NA, 2.19970703, NA), crea = c(1.19995117, 5.5, 2, 0.79992676), 
     sod = c(141, 132, 134, 139), ph = c(7.459961, 7.25, 7.459961, 
     NA), glucose = c(NA_real_, NA_real_, NA_real_, NA_real_), 
     bun = c(NA_real_, NA_real_, NA_real_, NA_real_), urine = c(NA_real_, 
     NA_real_, NA_real_, NA_real_), adlp = c(7, NA, 1, 0), adls = c(7, 
     1, 0, 0), sfdm2 = c(NA, "<2 mo. follow-up", "<2 mo. follow-up", 
     "no(M2 and SIP pres)"), adlsc = c(7, 1, 0, 0)), row.names = c(NA, 
 4L), class = "data.frame")

I have also made a graph of the SUPPORT day 3 physiology score (sps) grouped by the primary disease class (dzclass) with a DataFrame name of SB_xlsx.

SB_xlsx = SB_xlsx[!is.na(SB_xlsx$sps), ]
ggplot(SB_xlsx, aes(x=dzclass, y=sps)) + geom_boxplot() + ggtitle("Box Plot - sps by Primary Disease Class") + xlab("Disease Class") + ylab("sps")

Now I want to test whether the population mean SUPPORT day 3 physiology scores differ between the primary disease classes. I figured running pairwise t.tests would be a good option, but I'm having issues with my code.

mu.diff = 0
alpha = 0.05
combs = combn(unique(SB_xlsx$dzclass), 2)

for (s in 1:ncol(combs)) {
  i = combs[1, s]
  j = combs[2, s]
  tt = t.test(sps ~ dzclass, mu=mu.diff, var_equal=FALSE, 
              conf_level=1-alpha, alternative='two.sided', 
              data=SB_xlsx[dzclass == i | dzclass == j, ])
  print(tt)
}

This is the code I have so far. It should work in theory, but for some reason, I get an error "Error in eval(m$data, parent.frame()): object 'dzclass' not found". I'm confused by this since dzclass is clearly in my dataset, so I'm not sure how it's not found.

dzclass is not a known object when used in the data argument but is if used within the subset argument. Note that two-sided is the default so it does not have to be specified.

tt = t.test(sps ~ dzclass, data = SB_xlsx, subset = dzclass == i | dzclass == j,
  mu = mu.diff, var_equal = FALSE, conf_level = 1 - alpha)

Going further we can shorten the code like this:

for(s in as.data.frame(combs)) {
  tt = t.test(sps ~ dzclass, data = SB_xlsx, subset = dzclass %in% s, 
              mu = mu.diff, var_equal = FALSE, conf_level = 1 - alpha)
  print(tt)
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM