rep(no length.out = len):尝试在 R 中复制类型为“closure”的对象

rep(no length.out = len):attempt to replicate an object of type 'closure' in r

提问人:The_Wusman 提问时间:1/26/2023 最后编辑:Shawn HemelstrandThe_Wusman 更新时间:1/26/2023 访问量:56

问:

我正在尝试将函数循环到变量和 .该表仅返回 on 和 not 的操作结果,并显示以下消息:getLagSequencetpt2mclimate_variablestpt2m

 rep(no length.out = len):attempt to replicate an object of type 'closure' in r

以下是代码:

###### use the same iteration_length for all climate variables

  climate_variables <- c("tp","t2m")
 
  
  tmp_lag <- data.frame(Date=tmp$Date, HP=tmp$HP) %>% 
    mutate_if(is.factor, as.character)
  
  
  
  for (clim in climate_variables) {
    message('=== ',{clim},' ===== ')
    
    df1 <- tmp %>%  select(Date, Input = all_of(clim))
    o1 <- getLagSequences(df1$Input, 
                          aggr_function = ifelse(clim =="tp", sum, mean), # calculate the cumulative sum for precipitation, otherwise the mean for other variables
                          iteration = ifelse(clim == "t2m", iteration_length/2, iteration_length)) # only temperature use half of the iteration range
    colnames(o1$df_cumulative) <- colnames(o1$df_cumulative) %>% paste0(clim, "_",.)
    
    tmp_lag <- cbind(tmp_lag, o1$df_cumulative)
  }

谁能帮忙?

这里是:dput

structure(list(HP = c("Akosombo", "Akosombo", "Akosombo", "Akosombo", 
"Akosombo", "Akosombo", "Akosombo", "Akosombo", "Akosombo", "Akosombo", 
"Akosombo", "Akosombo", "Akosombo", "Akosombo", "Akosombo", "Akosombo", 
"Akosombo", "Akosombo", "Akosombo", "Akosombo", "Akosombo", "Akosombo", 
"Akosombo", "Akosombo", "Akosombo", "Akosombo", "Akosombo", "Akosombo", 
"Akosombo", "Akosombo", "Akosombo", "Akosombo", "Akosombo", "Akosombo", 
"Akosombo", "Akosombo", "Akosombo", "Akosombo", "Akosombo", "Akosombo", 
"Akosombo", "Akosombo", "Akosombo", "Akosombo", "Akosombo", "Akosombo", 
"Akosombo", "Akosombo", "Akosombo", "Akosombo", "Akosombo", "Akosombo", 
"Akosombo", "Akosombo", "Akosombo", "Akosombo", "Akosombo", "Akosombo", 
"Akosombo", "Akosombo", "Akosombo", "Akosombo", "Akosombo", "Akosombo", 
"Akosombo", "Akosombo", "Akosombo", "Akosombo", "Akosombo", "Akosombo", 
"Akosombo", "Akosombo", "Akosombo", "Akosombo", "Akosombo", "Akosombo", 
"Akosombo", "Akosombo", "Akosombo", "Akosombo", "Akosombo", "Akosombo", 
"Akosombo", "Akosombo", "Akosombo", "Akosombo", "Akosombo", "Akosombo", 
"Akosombo", "Akosombo", "Akosombo", "Akosombo", "Akosombo", "Akosombo", 
"Akosombo", "Akosombo", "Akosombo", "Akosombo", "Akosombo", "Akosombo", 
"Akosombo", "Akosombo", "Akosombo", "Akosombo", "Akosombo", "Akosombo", 
"Akosombo", "Akosombo", "Akosombo", "Akosombo", "Akosombo", "Akosombo", 
"Akosombo", "Akosombo", "Akosombo", "Akosombo", "Akosombo", "Akosombo", 
"Akosombo", "Akosombo", "Akosombo", "Akosombo", "Akosombo", "Akosombo", 
"Akosombo", "Akosombo", "Akosombo", "Akosombo", "Akosombo", "Akosombo", 
"Akosombo", "Akosombo", "Akosombo", "Akosombo", "Akosombo", "Akosombo", 
"Akosombo", "Akosombo", "Akosombo", "Akosombo", "Akosombo", "Akosombo", 
"Akosombo", "Akosombo", "Akosombo", "Akosombo", "Akosombo", "Akosombo", 
"Akosombo", "Akosombo", "Akosombo", "Akosombo", "Akosombo", "Akosombo", 
"Akosombo", "Akosombo", "Akosombo", "Akosombo", "Akosombo", "Akosombo", 
"Akosombo", "Akosombo", "Akosombo", "Akosombo", "Akosombo", "Akosombo", 
"Akosombo", "Akosombo", "Akosombo", "Akosombo", "Akosombo", "Akosombo", 
"Akosombo", "Akosombo", "Akosombo", "Akosombo", "Akosombo", "Akosombo", 
"Akosombo", "Akosombo", "Akosombo", "Akosombo", "Akosombo", "Akosombo", 
"Akosombo", "Akosombo", "Akosombo", "Akosombo", "Akosombo", "Akosombo", 
"Akosombo", "Akosombo", "Akosombo", "Akosombo", "Akosombo", "Akosombo", 
"Akosombo", "Akosombo", "Akosombo", "Akosombo", "Akosombo", "Akosombo", 
"Akosombo", "Akosombo", "Akosombo", "Akosombo", "Akosombo", "Akosombo", 
"Akosombo", "Akosombo", "Akosombo", "Akosombo", "Akosombo", "Akosombo", 
"Akosombo", "Akosombo", "Akosombo", "Akosombo", "Akosombo", "Akosombo", 
"Akosombo", "Akosombo", "Akosombo", "Akosombo", "Akosombo", "Akosombo", 
"Akosombo", "Akosombo", "Akosombo", "Akosombo", "Akosombo", "Akosombo", 
"Akosombo", "Akosombo", "Akosombo", "Akosombo", "Akosombo", "Akosombo", 
"Akosombo", "Akosombo", "Akosombo", "Akosombo", "Akosombo", "Akosombo", 
"Akosombo", "Akosombo", "Akosombo", "Akosombo", "Akosombo", "Akosombo", 
"Akosombo", "Akosombo", "Akosombo", "Akosombo", "Akosombo", "Akosombo", 
"Akosombo", "Akosombo", "Akosombo", "Akosombo", "Akosombo", "Akosombo", 
"Akosombo", "Akosombo", "Akosombo", "Akosombo", "Akosombo", "Akosombo", 
"Akosombo", "Akosombo", "Akosombo", "Akosombo", "Akosombo", "Akosombo", 
"Akosombo", "Akosombo", "Akosombo", "Akosombo", "Akosombo", "Akosombo", 
"Akosombo", "Akosombo", "Akosombo", "Akosombo", "Akosombo", "Akosombo", 
"Akosombo", "Akosombo", "Akosombo", "Akosombo", "Akosombo", "Akosombo", 
"Akosombo", "Akosombo", "Akosombo", "Akosombo", "Akosombo", "Akosombo", 
"Akosombo", "Akosombo", "Akosombo", "Akosombo", "Akosombo", "Akosombo", 
"Akosombo", "Akosombo", "Akosombo", "Akosombo", "Akosombo", "Akosombo", 
"Akosombo", "Akosombo", "Akosombo", "Akosombo", "Akosombo", "Akosombo", 
"Akosombo", "Akosombo", "Akosombo", "Akosombo", "Akosombo", "Akosombo", 
"Akosombo", "Akosombo", "Akosombo", "Akosombo", "Akosombo", "Akosombo", 
"Akosombo", "Akosombo", "Akosombo", "Akosombo", "Akosombo", "Akosombo", 
"Akosombo", "Akosombo", "Akosombo", "Akosombo", "Akosombo", "Akosombo", 
"Akosombo", "Akosombo", "Akosombo", "Akosombo", "Akosombo", "Akosombo", 
"Akosombo", "Akosombo", "Akosombo", "Akosombo", "Akosombo", "Akosombo", 
"Akosombo", "Akosombo", "Akosombo", "Akosombo", "Akosombo", "Akosombo", 
"Akosombo", "Akosombo", "Akosombo", "Akosombo", "Akosombo", "Akosombo", 
"Akosombo", "Akosombo", "Akosombo", "Akosombo", "Akosombo", "Akosombo", 
"Akosombo", "Akosombo"), Date = structure(c(5844, 5875, 5903, 
5934, 5964, 5995, 6025, 6056, 6087, 6117, 6148, 6178, 6209, 6240, 
6268, 6299, 6329, 6360, 6390, 6421, 6452, 6482, 6513, 6543, 6574, 
6605, 6634, 6665, 6695, 6726, 6756, 6787, 6818, 6848, 6879, 6909, 
6940, 6971, 6999, 7030, 7060, 7091, 7121, 7152, 7183, 7213, 7244, 
7274, 7305, 7336, 7364, 7395, 7425, 7456, 7486, 7517, 7548, 7578, 
7609, 7639, 7670, 7701, 7729, 7760, 7790, 7821, 7851, 7882, 7913, 
7943, 7974, 8004, 8035, 8066, 8095, 8126, 8156, 8187, 8217, 8248, 
8279, 8309, 8340, 8370, 8401, 8432, 8460, 8491, 8521, 8552, 8582, 
8613, 8644, 8674, 8705, 8735, 8766, 8797, 8825, 8856, 8886, 8917, 
8947, 8978, 9009, 9039, 9070, 9100, 9131, 9162, 9190, 9221, 9251, 
9282, 9312, 9343, 9374, 9404, 9435, 9465, 9496, 9527, 9556, 9587, 
9617, 9648, 9678, 9709, 9740, 9770, 9801, 9831, 9862, 9893, 9921, 
9952, 9982, 10013, 10043, 10074, 10105, 10135, 10166, 10196, 
10227, 10258, 10286, 10317, 10347, 10378, 10408, 10439, 10470, 
10500, 10531, 10561, 10592, 10623, 10651, 10682, 10712, 10743, 
10773, 10804, 10835, 10865, 10896, 10926, 10957, 10988, 11017, 
11048, 11078, 11109, 11139, 11170, 11201, 11231, 11262, 11292, 
11323, 11354, 11382, 11413, 11443, 11474, 11504, 11535, 11566, 
11596, 11627, 11657, 11688, 11719, 11747, 11778, 11808, 11839, 
11869, 11900, 11931, 11961, 11992, 12022, 12053, 12084, 12112, 
12143, 12173, 12204, 12234, 12265, 12296, 12326, 12357, 12387, 
12418, 12449, 12478, 12509, 12539, 12570, 12600, 12631, 12662, 
12692, 12723, 12753, 12784, 12815, 12843, 12874, 12904, 12935, 
12965, 12996, 13027, 13057, 13088, 13118, 13149, 13180, 13208, 
13239, 13269, 13300, 13330, 13361, 13392, 13422, 13453, 13483, 
13514, 13545, 13573, 13604, 13634, 13665, 13695, 13726, 13757, 
13787, 13818, 13848, 13879, 13910, 13939, 13970, 14000, 14031, 
14061, 14092, 14123, 14153, 14184, 14214, 14245, 14276, 14304, 
14335, 14365, 14396, 14426, 14457, 14488, 14518, 14549, 14579, 
14610, 14641, 14669, 14700, 14730, 14761, 14791, 14822, 14853, 
14883, 14914, 14944, 14975, 15006, 15034, 15065, 15095, 15126, 
15156, 15187, 15218, 15248, 15279, 15309, 15340, 15371, 15400, 
15431, 15461, 15492, 15522, 15553, 15584, 15614, 15645, 15675, 
15706, 15737, 15765, 15796, 15826, 15857, 15887, 15918, 15949, 
15979, 16010, 16040, 16071, 16102, 16130, 16161, 16191, 16222, 
16252, 16283, 16314, 16344, 16375, 16405, 16436, 16467, 16495, 
16526, 16556, 16587, 16617, 16648, 16679, 16709, 16740, 16770, 
16801, 16832, 16861, 16892, 16922, 16953, 16983, 17014, 17045, 
17075, 17106, 17136), class = "Date"), Input = c(26.0482682959188, 
29.7773414648134, 30.7658247023613, 31.9433871420554, 30.7364014593124, 
28.5660049693201, 27.008651300045, 26.7767901752569, 27.0106885279609, 
28.3049456994381, 27.6286551346249, 25.5781086040632, 27.0862878781908, 
29.802641804782, 30.9298179587514, 32.110381263161, 31.6466362120678, 
28.8888449460919, 27.8317727508966, 26.9869393159013, 27.6063324086927, 
28.4467615398805, 28.2010065643339, 26.5303358259993, 25.9467816615543, 
29.2783998300035, 31.4675279946574, 31.6308352847414, 31.123438747635, 
28.5134589772507, 26.9359544670438, 26.5175136498288, 26.9747457229744, 
28.7793425153922, 27.805185340739, 25.5495451870433, 25.141563558386, 
27.7657478983928, 30.2242588038482, 31.8135206113612, 31.2926440447558, 
28.8168465624867, 27.3689921327651, 26.5920963697783, 27.4579028453305, 
28.1827801214241, 28.1112035313647, 26.8368639810451, 27.3713481847507, 
28.686228607261, 30.9191891901141, 32.2907151516298, 30.8107776834469, 
29.2549615597846, 27.0963758490029, 27.1927465897801, 27.8244649782564, 
29.231052287163, 28.9700346096893, 28.0606718930205, 27.6025567137462, 
30.2173744202613, 31.4171121819406, 31.4070263870479, 29.5574125123695, 
29.1685392797126, 27.2979032363756, 26.6376271045924, 27.6632426927797, 
28.2425418592402, 27.7273825949657, 26.2956161840894, 25.8023976775792, 
29.3135782857728, 30.8568670263934, 31.5556583544317, 30.2917396917806, 
28.826192541469, 27.3511240009284, 26.5212985232929, 27.0049343157397, 
28.2711688847336, 27.0215770649634, 26.5156670772855, 25.4780578067307, 
28.7198755497594, 30.3443969434818, 31.6378434098386, 30.7156365515802, 
28.7692574674322, 26.8941413318764, 26.7800240351479, 27.3577370946678, 
28.6215657891664, 28.4441381440978, 26.4310100956665, 25.8494342399534, 
28.8841136226494, 31.039552285863, 31.4195110254553, 30.4823380747519, 
28.7412323540455, 27.3651897929287, 26.4590766344682, 27.043373979135, 
27.7380029023949, 27.2077451702823, 25.6077367592141, 26.3261607101168, 
28.7896924322736, 31.6543395416539, 32.0033418795745, 31.0777193287389, 
29.3483575416138, 27.6131108098797, 26.989845397369, 27.9148311799588, 
28.7928240337248, 27.92240247134, 27.7542744795733, 28.1825629975709, 
29.9824912834454, 31.7943997722801, 31.9185113746994, 30.7838628858826, 
29.0898469002322, 27.6355634804447, 26.8202464265104, 27.2731476365406, 
28.4058666570933, 27.2386528886223, 27.3242399118155, 28.1647117296143, 
28.8258677017827, 30.5842737822746, 31.6506041466067, 30.2519815230061, 
28.5145176556992, 27.6098190736943, 27.473417258537, 28.1678270809616, 
29.3070310029841, 28.6985295265985, 27.3024707706382, 27.5180381285211, 
30.3144979458478, 31.4652520862653, 32.8058989608865, 31.1835585012764, 
29.3126222249985, 27.7315834126542, 26.9100955012694, 27.5523901172946, 
28.9644641380706, 28.7799403737828, 27.6332468408283, 27.6579305972337, 
28.5214675981142, 31.708723935134, 31.7885437797179, 31.2317135793462, 
29.6140584084973, 27.4975589893936, 26.5566098437212, 26.9594314832407, 
28.464668474752, 28.4606017472814, 26.5738953356549, 28.0248212937307, 
28.2479958966852, 30.5889630661028, 32.1540101172379, 30.8606144643313, 
28.8847217370098, 27.1204364278393, 26.7093928295751, 27.5077124430203, 
28.4460264686389, 28.2758170460637, 26.5025144088951, 26.3312531540967, 
28.3215080321432, 31.1462508991463, 32.0407532575691, 31.0655502363351, 
28.8129720018024, 27.3316167570576, 26.9468994434119, 27.2199801227571, 
28.9824720917124, 28.6146200565848, 27.9026925515234, 27.1676210413039, 
29.2129337349599, 32.0146262670031, 32.0272812316246, 31.4629315638415, 
29.3095204726139, 27.7723715490099, 26.9736620955171, 27.5100517952406, 
28.178621012688, 28.2060695754962, 26.9285905846535, 27.545049473166, 
30.7454755475608, 31.4272264464449, 32.0091319984266, 31.4081451431357, 
28.7307007929842, 27.3541790222564, 27.0197798190545, 27.5920672232544, 
29.2867848102304, 28.6729450000046, 27.3266996951181, 27.3428965810905, 
29.757919114736, 31.1985001059056, 31.8593776094795, 30.7568726641768, 
28.9917963122669, 27.4238058195234, 27.1031943229543, 27.7623552569315, 
29.26119556766, 28.8126135919223, 28.5732688568003, 26.9761678487728, 
31.1937042971011, 32.6017938468473, 32.6904760313217, 31.3649024273158, 
29.1342774690458, 27.5243165976797, 26.9348094107457, 27.9463564740596, 
28.9885866027483, 28.6460422873679, 27.7937683966546, 27.6396106409292, 
30.1125704871269, 31.98068866556, 32.5405942608665, 31.0212643870941, 
29.4966811589324, 28.0052555509314, 27.139294441008, 27.3397249427638, 
28.7861480451755, 27.5720049093274, 27.0327221686787, 26.8527895582812, 
29.7606337927226, 31.7644126568348, 31.7924255510104, 30.7821291370237, 
29.6929916919269, 27.62876515691, 26.7178499661993, 27.6440570110847, 
29.0125324545003, 28.4457590875551, 26.9508400149845, 25.0974867725926, 
29.1452968253411, 31.1215873214423, 31.5831582695398, 30.8546912629774, 
29.29584586955, 27.1853445013247, 26.6651088182347, 27.5780903917659, 
28.7101885888857, 28.0300172485431, 27.1651737093163, 26.0545031723997, 
30.1770750410007, 31.6401997861799, 31.8554181208314, 31.158956866923, 
29.2047430900737, 27.6191184312874, 27.2355494300994, 27.5927120642657, 
28.6820145484734, 28.0662279380691, 27.4269154051711, 27.6400709138959, 
31.1259663261408, 32.3394463746391, 32.4378279666864, 31.2648880274925, 
29.3791651846817, 27.5947345677527, 26.9851049439804, 27.3199542217825, 
28.5484849559789, 28.6803674180821, 26.9958594246695, 26.7591473517798, 
29.4725599540334, 31.9409925094035, 31.8449587888571, 31.0519689860574, 
29.3585647377984, 27.4336356964026, 26.7927739700833, 27.4922246773117, 
28.7788444173942, 28.1935845716918, 26.3576943771665, 26.3448847235135, 
29.2663348658106, 31.4875284549996, 31.8044215474349, 30.5288746582356, 
29.0585283965955, 27.2758279351503, 26.9731354088572, 27.3209068769329, 
28.6424517774401, 29.0842001434876, 27.1815259941253, 26.6499917356935, 
29.6864481394507, 32.4428992689595, 31.6973858518292, 31.0146189300291, 
29.2758170359082, 27.519639349594, 26.8856739514651, 27.4930611831246, 
28.7922528105408, 28.6967123480027, 26.9677560007178, 27.5656928830295, 
29.42518616944, 31.2693899207395, 31.8146268393419, 30.7418394887036, 
29.4079365571017, 28.0051736550693, 26.9564453995355, 27.4747040979905, 
28.9436305923457, 28.9765485685434, 27.2845329022319, 26.3726517155366, 
30.2886908275224, 31.4040025302079, 32.2693761408674, 31.911187815594, 
29.7569474333048, 27.9704883956223, 26.9649706237808, 27.5706740772423, 
29.0480452166754, 28.4667000899175, 26.5123045842575, 26.8824944846716, 
29.7546356926643, 31.7303156891573, 32.6733488867485, 31.4315277372698, 
29.2899414407299, 27.7641323782071, 27.0291545066485, 27.7032782050942, 
29.5229940726649, 29.0597468627416, 27.905663246901)), class = c("grouped_df", 
"tbl_df", "tbl", "data.frame"), row.names = c(NA, -372L), groups = structure(list(
    HP = "Akosombo", .rows = structure(list(1:372), ptype = integer(0), class = c("vctrs_list_of", 
    "vctrs_vctr", "list"))), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -1L), .drop = TRUE))
R 循环时间 序列 闭包 滞后

评论

0赞 Roland 1/26/2023
我无法测试,因为您没有提供可重复的示例。但是,我很确定这是因为您(错误地)使用了 .我可以用 重现此错误消息。因此,不知何故,该调用中的条件似乎长度> 1。您应该使用 和 而不是 .但是您仍然会遇到您的病情不是长度为 1 的问题。不过,我不清楚为什么会这样。ifelseifelse(c(FALSE, FALSE), sum, mean)ifelseifelseifelse
0赞 Shawn Hemelstrand 1/26/2023
欢迎来到 SO!通常建议包括一个带有问题的可重复数据集。您可以运行输出并将其粘贴到您的问题中吗?不幸的是,屏幕截图和表格不是很有帮助。dput(df1)
2赞 The_Wusman 1/26/2023
@ShawnHemelstrand 谢谢。结果已按要求粘贴

答: 暂无答案