计算2年的差异

时间:2015-02-03 00:28:24

标签: r

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

代码段:

## 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.

dput():

名称= c(“playerID”,“yearID”,“teamID”,“lgID”,“stint”,“G”,“AB”,“R”,“SO”,“IBB”,“HBP” ,“SH”,“SF”,“GIDP”,“工资”,“生育年”,“出生月”,“出生日”,“出生国家”,“出生国家”,“出生城市”,“死亡年”,“死亡月”,“ 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 =)&gt; -

1 个答案:

答案 0 :(得分:1)

我在你的代码中看到了拼写错误(小而大&#34; D&#34;字母在&#34; ID&#34;):

我已确认两者

merged.all$yearID and merged.all$birthYear 

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