Error: object not found, when using Table1 function.

Hi all,
I'm trying to generate a descriptive stats table for the variable age_arrest from the data frame "Data", however I get an error message after I run table1() saying:

Error in .table1.internal(x = x, labels = labels, groupspan = groupspan, :
object 'X1st_four' not found

I thought the Table1 function helps access the variables (e.g. X1st_four) I need from the dataframe without having to write "Data$X1st_four". It's strange since I'll restart R or do something unrelated and then run the same code again and then the error will be resolved. But then it comes back and I'm not sure why. I'd appreciate help looking through my code and identifying what the problem is. Thanks!

rm(list = ls())
library(tidyverse)
library(dplyr)
library(plyr)
library(MASS)
library(lubridate)
library(caret)
library(leaps)
library(furniture)
library(readxl)

##load data
Data <- read_excel("~/iCloud Drive (Archive) - 1//Data.xlsx")

Data$X1st_four<-as.numeric(Data$first_four)
Data$last_four<-as.numeric(Data$last_four)
Data$X1st_gcs_motor<-as.numeric(Data$first_gcs_motor)
Data$time_to_rosc<-as.numeric(Data$time_to_rosc)

library(magrittr)
Data$pupil1<-ifelse(Data$first_pupils=='Both present'| Data$first_pupils=='One absent',1,0)%>%as.factor()
Data$corneal1<-ifelse(Data$first_corneals=='Both present'|Data$first_corneals=='One absent',1,0)%>%as.factor()
Data$cough1<-ifelse(Data$first_cough=='Present',1,0)%>%as.factor()
Data$mental1<-ifelse(Data$first_mental=='Comatose',0,1)%>%as.factor()
Data$breathing1<-ifelse(Data$first_breathing=='Breathing over ventilator',1,0)%>%as.factor()

Data$arrest_location_simple<-as.factor(Data$arrest_location_simple)
levels(Data$arrest_location_simple) <- gsub("Out of Hospital","OOH", levels(Data$arrest_location_simple))
levels(Data$arrest_location_simple) <- gsub("OOH ","OOH", levels(Data$arrest_location_simple))
levels(Data$arrest_location_simple) <- gsub("ED","OOH", levels(Data$arrest_location_simple))

library(table1)
table<-table1(Data,X1st_four,age_arrest,gender, arrest_location_simple, multiple_arrests,myoclonus,first_ct_result,pupil1,corneal1,mental1,breathing1,
cough1,first_gcs_motor,ct_repeat,
splitby = 'death',
second = c('age_arrest','first_gcs_motor', 'X1st_four'),
total=FALSE,
row_wise =FALSE,
test = TRUE,
param = TRUE,
header_labels = NULL,
type = "pvalues")

I can't see anything wrong in your code. It could be something in the data.

Also, you are loading a lot of packages, I bet some of them mask functions from other packages. Maybe remove them all and just add them in one at a time when you need them. In particular, I never load "tidyverse", but only the individual packages as I use them.

When you load packges you will get a warning if it masks a function from another packages. For example two of your packages have a table1 function. You can specify which one to use like furniture::table1

1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.