TimeSplitter: eval(substitute(exit), data, parent.frame()) 错误:找不到对象 'Age'

TimeSplitter: Error in eval(substitute(exit), data, parent.frame()) : object 'Age' not found

我是R的新手,如果你能解决我的问题,我将不胜感激。

这是我的代码:

dat1 <- read.csv("data.csv",header=T)
spl_dat1 <-
     dat1 %>%
     timeSplitter(by = 5,
                  time_var = "Age",
                  event_var = "Alive",
                  event_start_status = "1",
                  time_related_vars = c("Born", "Death"))

这是我的数据库图片: enter image description here

这是我的dput(head(dat1))

Born = c(1949L,1949L, 1949L, 1949L, 1949L, 1949L), 
Death = c(1970L, 1954L, 1954L,1954L, 1954L, 1968L), 
Age = c(22, 6, 6, 6, 6, 20), 
Alive = c(0L, 0L, 0L, 0L, 0L, 0L), 
Type = structure(c(3L, 5L, 5L, 5L, 5L, 5L), 
Label = c("AdministrativeOffices", "DepartmentsDSC", "GeneralOffice", 
"InstitutionsDSC", "Ministries", "NationalBureausAMC"), class = "factor"),
GeneralOffice = c(1L, 0L, 0L, 0L, 0L, 0L), 
Ministries = c(0L, 1L, 1L, 1L, 1L, 1L), 
DepartmentsDSC = c(0L, 0L, 0L, 0L, 0L, 0L), 
AdministrativeOffices = c(0L, 0L, 0L, 0L, 0L, 0L), 
NationalBureausAMC = c(0L, 0L, 0L, 0L, 0L, 0L), 
InstitutionsDSC = c(0L, 0L, 0L, 0L, 0L, 0L), 
Law = structure(c(2L, 2L, 2L, 2L, 2L, 2L), .Label = c("CentralCommitteeoftheCPC", "NationalPeoplesCongress", "NPCStandingCommittee", "StateCouncilMeeting"), class = "factor"),
 NationalPeoplesCongress = c(1L, 1L, 1L, 1L, 1L, 1L), 
NPCStandingCommittee = c(0L, 0L, 0L, 0L, 0L, 0L),
 CentralCommitteeoftheCPC = c(0L, 0L, 0L, 0L, 0L, 0L),
 StateCouncilMeeting = c(0L, 0L, 0L, 0L, 0L, 0L),
 Function = structure(c(3L, 3L, 4L, 3L, 3L, 3L), .Label = c("EconomicManagement", "EnforcementSupervision", "GovernmentOffices", "MacroRegulation", "SocialAffairs"), class = "factor")

这是structure(dat1):

Name Born Death Age AgeGroup Alive       Type   
1    1949  1970  22        2     0 Ministries                                            
2    1949  1954   6        1     0 Ministries          
3    1949  1954   6        1     0 Ministries   
4    1949  1954   6        1     0 Ministries   
5    1949  1954   6        1     0 Ministries
6    1949  1968  20        2     0 Ministries
7    1949  2018  70        3     1 Ministries
8    1949  2018  70        3     1 Ministries 
9    1949  1959  11        2     0 Ministries 
10   1949  2018  70        3     1 Ministries
11   1949  1952   4        1     0 Ministries 

但是我运行代码的时候,出现了错误。

Error in eval(substitute(exit), data, parent.frame()) : object 'Age' not found In addition: Warning message: In max(data[[time_var]]) : no non-missing arguments to max; returning -Inf

我的数据库中有一个 "Age" 列,它是数字。我不明白这里出了什么问题。

问题是 event_var 变量需要是因子(或字符)class。该示例还有一个问题,因为它只有一个级别用于状态变量。如果您允许某些 Alive 向量为 1 并将其作为因数,则不会出错。这显然是 Greg 包文档中的一个遗漏。小插图示例没有提及要求,但不会 运行 出错,因为 data.frame 的默认值是将字符值转换为因子,并且示例使用字符向量。

dat1 <- data.frame(

Born = c(1949L,1949L, 1949L, 1949L, 1949L, 1949L), 
Death = c(1970L, 1954L, 1954L,1954L, 1954L, 1968L), 
Age = c(22, 6, 6, 6, 6, 20), 
Alive = factor( c(0L, 0L, 0L, 1L, 0L, 1L)), 
Type = structure(c(3L, 5L, 5L, 5L, 5L, 5L), 
                 Label = c("AdministrativeOffices", "DepartmentsDSC", "GeneralOffice", 
                           "InstitutionsDSC", "Ministries", "NationalBureausAMC"), class = "factor"),
GeneralOffice = c(1L, 0L, 0L, 0L, 0L, 0L), 
Ministries = c(0L, 1L, 1L, 1L, 1L, 1L), 
DepartmentsDSC = c(0L, 0L, 0L, 0L, 0L, 0L), 
AdministrativeOffices = c(0L, 0L, 0L, 0L, 0L, 0L), 
NationalBureausAMC = c(0L, 0L, 0L, 0L, 0L, 0L), 
InstitutionsDSC = c(0L, 0L, 0L, 0L, 0L, 0L), 
Law = structure(c(2L, 2L, 2L, 2L, 2L, 2L), .Label = c("CentralCommitteeoftheCPC", "NationalPeoplesCongress", "NPCStandingCommittee", "StateCouncilMeeting"), class = "factor"),
NationalPeoplesCongress = c(1L, 1L, 1L, 1L, 1L, 1L), 
NPCStandingCommittee = c(0L, 0L, 0L, 0L, 0L, 0L),
CentralCommitteeoftheCPC = c(0L, 0L, 0L, 0L, 0L, 0L),
StateCouncilMeeting = c(0L, 0L, 0L, 0L, 0L, 0L),
Function = structure(c(3L, 3L, 4L, 3L, 3L, 3L), .Label = c("EconomicManagement", "EnforcementSupervision", "GovernmentOffices", "MacroRegulation", "SocialAffairs"), class = "factor"))

spl_dat1 <-
    dat1 %>%
    timeSplitter(by = 5,
                 time_var = "Age",
                 event_var = "Alive",
                 event_start_status = "1",
                 time_related_vars = c("Born", "Death"))

没有错误。该代码仅强制使用此代码对字符变量进行分解:

if (is.character(data[[event_var]])) 
    data[[event_var]] <- factor(data[[event_var]])

...但不对数字或整数 classed 变量执行所需的强制转换。