【问题标题】:Calculate the difference in 2 years计算2年的差异
【发布时间】:2015-04-02 02:01:33
【问题描述】:

一年是出生年,另一年是季节年;目标是计算特定季节的年龄(以年为单位)。我想获取此信息并在合并数据集中创建一个新字段 (master$playerAge)。我收到一个不熟悉的错误,不确定是什么原因造成的。我已经确认merged.all$yearIDmerged.all$birthYear 都存在于merged.all 中。任何帮助表示赞赏。

代码sn-p:

## calculate age from birthYear and YearId
master$playerAge <- paste(merged.all$yearId - merged.all$birthYear)

Error:
Error in `[<-.data.table`(x, j = name, value = value) : 
  RHS of assignment to new column 'playerAge' is zero length 
  but not empty list(). For new columns the RHS must either be 
  empty list() to create an empty list column, or, have length > 0; 
  e.g. NA_integer_, 0L, etc.

输入():

Names = c("playerID", "yearID", "teamID", "lgID", "stint", "G", "AB", "R", "SO", "IBB", "HBP" 、“SH”、“SF”、“GIDP”、“salary”、“birthYear”、“birthMonth”、“birthDay”、“birthCountry”、“birthState”、“birthCity”、“deathYear”、“deathMonth”、“ deathDay”、“deathCountry”、“deathState”、“deathCity”、“nameFirst”、“nameLast”、“nameGiven”、“weight”、“height”、“bats”、“throws”、“debut”、“finalGame” , "retroID", "bbrefID", "name", "dob"), sorted = "playerID", class= c("data.table", "data.frame"), row.names = c(NA, - 6L), .internal.selfref = ) > –

【问题讨论】:

  • 我不知道你为什么要使用 paste() 函数。如果 'yearID' 和 'birthYear' 是 POSIXct 类型,那么你可以减去它们,不需要 paste()。
  • @Michael - 感谢您的提示。我已经删除了。
  • 您应该将dput 编辑到您的问题中,这很糟糕。

标签: r


【解决方案1】:

我在您的代码中看到了拼写错误(“ID”中的大小“D”字母):

我都确认了

merged.all$yearID and merged.all$birthYear 

master$playerAge <- paste(merged.all$yearId - merged.all$birthYear)

【讨论】:

    猜你喜欢
    • 2020-09-26
    • 2018-04-24
    • 2020-03-26
    • 2020-02-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-11
    • 1970-01-01
    相关资源
    最近更新 更多