R语言 如何在data.table中使用列索引而不是列名

jgwigjjp  于 2023-07-31  发布在  其他
关注(0)|答案(1)|浏览(132)

这是密码
我正在处理的数据表:

structure(list(`Timestamp (America/Denver)` = structure(c(1689769414, 
1689769426, 1689769438, 1689769450, 1689769462, 1689769474, 1689769486, 
1689769499, 1689769511, 1689769523, 1689769534, 1689769547, 1689769559, 
1689769570, 1689769583, 1689769595, 1689769607, 1689769618, 1689769630, 
1689769642, 1689769654, 1689769666, 1689769678, 1689769690, 1689769703, 
1689769714, 1689769726, 1689769739, 1689769750, 1689769763, 1689769774, 
1689769786, 1689769799, 1689769810, 1689769823, 1689769835, 1689769847, 
1689769860, 1689769871, 1689769884, 1689769895, 1689769907, 1689769919, 
1689769931, 1689769944, 1689769956, 1689769968, 1689769982, 1689769995, 
1689770021, 1689770035, 1689770047, 1689770059, 1689770072, 1689770083, 
1689770096, 1689770107, 1689770119, 1689770132, 1689770143, 1689770155, 
1689770168, 1689770181, 1689770197, 1689770208, 1689770221, 1689770233, 
1689770246, 1689770257, 1689770270, 1689770282, 1689770293, 1689770306, 
1689770318, 1689770329, 1689770342, 1689770354, 1689770384, 1689770395, 
1689770407, 1689770420, 1689770432, 1689770444, 1689770456, 1689770469, 
1689770482, 1689770493, 1689770505, 1689770517, 1689770530, 1689770542, 
1689770555, 1689770568, 1689770579, 1689770591, 1689770603, 1689770615, 
1689770628, 1689770640, 1689770652, 1689770664, 1689770677, 1689770688, 
1689770702, 1689770765, 1689770777, 1689770789, 1689770801, 1689770814, 
1689770826, 1689770837, 1689770849, 1689770861, 1689770873, 1689770885, 
1689770897, 1689770909, 1689770921, 1689770934, 1689770946, 1689770957, 
1689770970, 1689770982, 1689770994, 1689771006, 1689771019, 1689771031, 
1689771043, 1689771055, 1689771067, 1689771080, 1689771093, 1689771123, 
1689771135, 1689771147, 1689771159, 1689771171, 1689771184, 1689771196, 
1689771209, 1689771221, 1689771234, 1689771245, 1689771257, 1689771269, 
1689771281, 1689771294, 1689771305, 1689771317, 1689771330, 1689771341, 
1689771354, 1689771365, 1689771377, 1689771390, 1689771402, 1689771414, 
1689771426, 1689771439, 1689771452, 1689771484, 1689771496, 1689771508, 
1689771521, 1689771534, 1689771546, 1689771557, 1689771570, 1689771582, 
1689771595, 1689771607, 1689771619, 1689771632, 1689771663, 1689771676, 
1689771689, 1689771701, 1689771712, 1689771725, 1689771737, 1689771749, 
1689771762, 1689771776, 1689771788, 1689771801, 1689771813, 1689771842, 
1689771855, 1689771867, 1689771880, 1689771892, 1689771904, 1689771916, 
1689771928, 1689771941, 1689771953, 1689771966, 1689771978, 1689771989, 
1689772001, 1689772014, 1689772025, 1689772037, 1689772050, 1689772063, 
1689772075, 1689772086, 1689772099, 1689772110, 1689772123, 1689772136, 
1689772147, 1689772161, 1689772172, 1689772202, 1689772214, 1689772226, 
1689772239, 1689772251, 1689772263, 1689772276, 1689772289, 1689772301, 
1689772313, 1689772325, 1689772338, 1689772350, 1689772361, 1689772374, 
1689772385, 1689772397, 1689772410, 1689772421, 1689772433, 1689772445, 
1689772458, 1689772470, 1689772482, 1689772496, 1689772507, 1689772521, 
1689772532, 1689772562, 1689772574, 1689772586, 1689772599, 1689772611, 
1689772623, 1689772636, 1689772649, 1689772660, 1689772673, 1689772685, 
1689772697, 1689772709, 1689772721, 1689772734, 1689772745, 1689772757, 
1689772770, 1689772781, 1689772793, 1689772806, 1689772817, 1689772830, 
1689772842, 1689772855, 1689772868, 1689772880, 1689772892, 1689772923, 
1689772935, 1689772947, 1689772960, 1689772972, 1689772984, 1689772996, 
1689773008), class = c("POSIXct", "POSIXt"), tzone = "UTC"), 
    `Heat_Capacity` = c(5.5, 6, 6.5, 7, 7.5, 
    7, 6.5, 6, 5.5, 5, 4.5, 4, 3.5, 3, 2.5, 2, 1.5, 1, 0.5, 0, 
    0.5, 1, 1.5, 2, 2.5, 3, 3.5, 4, 4.5, 5, 5.5, 6, 6.5, 7, 7.5, 
    6.96, 6.46, 5.96, 5.46, 4.96, 4.46, 3.96, 3.46, 2.96, 2.46, 
    1.92, 1.42, 0.83, 0.29, 0.79, 1.33, 1.88, 2.38, 2.88, 3.38, 
    3.88, 4.38, 4.88, 5.38, 5.88, 6.38, 6.92, 7.42, 6.92, 6.42, 
    5.92, 5.38, 4.88, 4.38, 3.88, 3.38, 2.88, 2.38, 1.88, 1.38, 
    0.88, 0.38, 0.88, 1.38, 1.88, 2.42, 2.92, 3.42, 3.92, 4.46, 
    4.96, 5.46, 5.96, 6.46, 6.96, 7.46, 6.96, 6.46, 5.96, 5.46, 
    4.96, 4.46, 3.96, 3.46, 2.96, 2.46, 1.92, 1.42, 0.88, 1, 
    1.5, 2, 2.5, 3, 3.5, 4, 4.5, 5, 5.5, 6, 6.5, 7, 7.5, 7, 6.5, 
    6, 5.5, 4.96, 4.46, 3.96, 3.46, 2.96, 2.46, 1.92, 1.42, 0.88, 
    0.38, 0.88, 1.38, 1.92, 2.42, 2.92, 3.42, 3.92, 4.5, 5, 5.5, 
    6, 6.5, 7, 7.5, 7, 6.5, 6, 5.5, 5, 4.5, 4, 3.5, 3, 2.5, 2, 
    1.46, 0.96, 0.42, 0.92, 1.46, 1.96, 2.5, 3, 3.5, 4, 4.5, 
    5.04, 5.54, 6.08, 6.58, 7.08, 6.58, 6.08, 5.54, 5.04, 4.54, 
    4.04, 3.5, 3, 2.46, 1.92, 1.42, 0.88, 0.38, 0.88, 1.42, 1.92, 
    2.42, 2.92, 3.42, 3.92, 4.46, 4.96, 5.5, 6, 6.5, 7, 7.5, 
    7, 6.5, 6, 5.46, 4.96, 4.46, 3.96, 3.46, 2.96, 2.46, 1.92, 
    1.42, 0.88, 0.38, 0.88, 1.38, 1.88, 2.38, 2.88, 3.42, 3.92, 
    4.46, 4.96, 5.46, 6, 6.5, 7, 7.5, 7, 6.5, 6, 5.5, 5, 4.5, 
    4, 3.5, 2.96, 2.46, 1.92, 1.42, 0.88, 0.38, 0.88, 1.38, 1.88, 
    2.42, 2.92, 3.42, 3.92, 4.46, 4.96, 5.46, 5.96, 6.5, 7, 7.5, 
    7, 6.5, 6, 5.5, 5, 4.5, 4, 3.5, 3, 2.46, 1.92, 1.42, 0.88, 
    0.38, 0.88, 1.42, 1.92, 2.42, 2.92, 3.42, 3.92, 4.46)), row.names = c(NA, 
-278L), class = c("data.table", "data.frame"), .internal.selfref = <pointer: 0x0000020e1d67b610>)

字符串
添加了示例数据

date_range <- one[,round(range(`Timestamp (America/New York`)+c(-60*60, 60*60), unit="hours")]


在表中,oneTimestamp (America/New York是第一列。
我如何指向上面的列索引,而不是像上面的代码中那样放置列名。

编辑

date_range <- one[,round(range(.SD[,1]+c(-60*60, 60*60), unit="hours"))]


舍入误差(范围(.SD[,1] + c(-60 * 60,60 * 60),单位=“小时”)):数学函数的非数值参数

smdncfj3

smdncfj31#

使用示例数据并比较代码之间的结果,并将.SD子集设置在one中,就像列表.SD[[1]]一样,因为range/round函数需要向量输入:

one[,round(range(`Timestamp (America/Denver)`)+c(-60*60, 60*60), unit="hours")]
##[1] "2023-07-19 11:00:00 UTC" "2023-07-19 14:00:00 UTC"

one[,round(range(.SD[[1]])+c(-60*60, 60*60), unit="hours")]
##[1] "2023-07-19 11:00:00 UTC" "2023-07-19 14:00:00 UTC"

字符串

相关问题