r - TimeSplitter:评估错误(替代(退出)、数据、父框架()):找不到对象“年龄”
问题描述
我是 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"))
这是我的数据库图片: 在此处输入图片描述
这是我的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
但是当我运行代码时,发生了错误。
eval(substitute(exit), data, parent.frame()) 中的错误:找不到对象'Age' 另外:警告消息:在 max(data[[time_var]]) 中:max 没有非缺失参数;返回-Inf
我的数据库中有一个“年龄”列,它是数字的。我无法理解这里出了什么问题。
解决方案
问题是event_var
变量需要是因子(或字符)类。这个例子还有一个问题,因为它只有一个级别的状态变量。如果您允许某些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]])
...但不对数字或整数分类变量执行所需的强制。
推荐阅读
- static - 如何在 React Native 的组件中导出和导入静态?
- c# - 如何在 C# Task.Factory.StartNew 中等待几秒钟
- robotframework - 哪种模型更适合机器人框架实现?
- email - 使用 groovy 在 Jmeter 中根据收件人电子邮件 ID 读取电子邮件
- python - 用于 python 的 Azure eventthub 库
- javascript - 如何在我的侧边栏中悬停时进行动态下拉?
- java - Java 正则表达式必须在字符串的开头或结尾匹配
- javascript - 选择上的下拉按钮禁用一些第二个下拉选项
- python - 矩阵未对齐错误消息激发确认矩阵的适当尺寸
- python - 如何在 AWS CDK 中创建侦听器规则?