错误“要替换的项目数不是替换长度的倍数”R

时间:2015-07-28 14:58:03

标签: r

平, 我知道这个错误有一些线索,但到目前为止还没有回答我的问题,所以我想我会试一试。 我的数据框看起来基本上是这样的:

enter image description here

现在我想用同一个Transect的温度填充“prev.temp”列,同一年,同一季度的最后一年。我跳过2008年,因为这是第一年(所以没有去年的数据),然后我告诉R:

weather_data$prev.temp <- NA
num.2008 <- nrow(weather_data[weather_data$year==2008,])
for (i in 1:(nrow(weather_data)-num.2008)){
  Year <- weather_data$year[i+num.2008]-1
  trans.id <- weather_data$transect_id[i+num.2008]
  quarter <- weather_data$Quarter[i+num.2008]
  weather_data$prev.temp[i+num.2008] <- weather_data$LST[weather_data$transect_id==trans.id&weather_data$year==Year&weather_data$Quarter==quarter]}

但我得到的只是警告

    > warnings()
Warnmeldungen:
1: In weather_data$prev.temp[i + num.2008] <- weather_data$LST[weather_data$transect_id ==  ... :
  number of items to replace is not a multiple of replacement length
2: In weather_data$prev.temp[i + num.2008] <- weather_data$LST[weather_data$transect_id ==  ... :
  number of items to replace is not a multiple of replacement length
3: In weather_data$prev.temp[i + num.2008] <- weather_data$LST[weather_data$transect_id ==  ... :
  number of items to replace is not a multiple of replacement length
4: In weather_data$prev.temp[i + num.2008] <- weather_data$LST[weather_data$transect_id ==  ... :
  number of items to replace is not a multiple of replacement length
5: In weather_data$prev.temp[i + num.2008] <- weather_data$LST[weather_data$transect_id ==  ... :
  number of items to replace is not a multiple of replacement length

依此类推数据框中的所有七千条线。有人知道为什么?希望这不是一个愚蠢的问题,我对R来说很新......

提前致谢!

编辑:我的数据集中的几行(保存为.csv)

“transect_id”, “年”, “日”, “月”, “善堂”, “沉淀”, “季”, “SumPre”, “average.temp” “TR001”,2008,1,1,11.47578335,0,1,83.298453932,13.9583943181648 “TR002”,2008,1,1,11.47576523,0,1,83.298453932,13.8530953324835 “TR003”,2008,1,1,11.49407959,0,1,83.298453932,13.9139920434176 “TR004”,2008,1,1,13.2079916,0,1,86.926050666,14.5018122427143 “TR006”,2008,1,1,10.91125584,0,1,90.573905345,13.0206362548352 “TR007”,2008,1,1,6.603372097,0,1,77.737346644,10.3549168582747 “TR009”,2008,1,1,13.25433731,0,1,33.859304729,14.9906256856923 “TR010”,2008,1,1,13.29336548,0,1,29.724921671,15.8152628050659 “TR011”,2008,1,1,12.42789459,0,1,42.512791991,14.1881363365165 “TR012”,2008,1,1,10.8834753,0,1,92.753992081,13.8890411936044 “TR013”,2008,1,1,10.96796608,0,1,92.753992081,13.9739660012857 “TR014”,2008,1,1,10.92393589,0,1,92.753992081,13.9506789353297 “TR016”,2008,1,1,10.54233265,0,1,34.741512,12.172537263967 “TR018”,2008,1,1,10.43720245,0,1,34.741512,12.022837968989

...

“TR016”,2013,365,12,10.23799419,0,4,50.94453413,17.3295336755543 “TR018”,2013,365,12,9.642765999,0,4,50.94453413,17.2109681291522 “TR019”,2013,365,12,12.99611282,0,4,128.850266687,19.7414660293043 “TR020”,2013,365,12,17.03261375,9.768366814,4,376.689895288,23.1223101931522 “TR023”,2013,365,12,12.33310318,0,4,136.517908337,20.1139735760435 “TR025”,2013,365,12,12.97974873,0,4,74.026901609,20.4519523951522 “TR027”,2013,365,12,11.9024229,0,4,139.080408216,20.105034704 “TR029”,2013,365,12,13.14897919,0,4,123.187800423,20.2958238021087 “TR030”,2013,365,12,11.91250134,0,4,139.080408216,20.0771734509239 “TR031”,2013,365,12,11.69286346,0,4,139.080408216,20.0171743888696 “TR032”,2013,365,12,11.47259903,0,4,139.080408216,20.0680306065326 “TR033”,2013,365,12,11.97963524,0,4,117.062066789,18.3602539717283 “TR034”,2013,365,12,14.35598278,0,4,119.927000758,20.1186091068696 “TR035”,2013,365,12,10.59048462,2.074222326,4,238.580411904,18.9340195089891 “TR037”,2013,365,12,12.20473003,13.27494144,4,299.027472969,19.9794068390978 “TR038”,2013,365,12,11.80709839,0,4,133.453267087,19.8448736253152 “TR039”,2013,365,12,13.96679783,0,4,48.515721084,21.041108784413 “TR040”,2013,365,12,9.902122498,0,4,50.94453413,17.425598701913 “TR041”,2013,365,12,10.84326458,0,4,49.502096769,17.157990155663 “TR042”,2013,365,12,NA,5.120532513,4,113.003856957,NA “TR044”,2013,365,12,13.19787312,0,4,120.363720062,19.4256780770978 “TR045”,2013,365,12,11.66844082,6.284669876,4,395.10155391,18.9535037953587 “TR050”,2013,365,12,13.73744583,19.49760818,4,585.042889358,21.1455532992391 “TR051”,2013,365,12,13.7811594,19.49760818,4,585.042889358,21.2266603652174 “TR052”,2013,365,12,14.04342079,5.446793079,4,42.28728497,20.9827080707283 “TR053”,2013,365,12,12.27605724,0,4,82.855937481,19.324892184163 “TR054”,2013,365,12,13.81909752,0,4,114.275216343,20.4377782650978

第一行和最后一行。对不起,如果你没有数据集就说不出来,那我就不是浪费你的时间了!

0 个答案:

没有答案