将data.table转换为data.frame(即undo setDT) [英] Convert data.table to data.frame (i.e. undo setDT)
问题描述
我有一个20x2的数据帧。我将该数据帧转换为data.table以执行一些操作(删除了对操作和目标的解释,超出了范围)。转换使我避免使用for循环。但是转换会产生一些问题。
I have a 20x2 dataframe. I converted that dataframe to a data.table to perform some operations (deleted the explanation of the operations and goal as out of scope). The conversion allowed me to avoid using a for loop. But the conversion generates some issues down the line.
我需要将df data.table转换回data.frame。我怎样才能做到这一点?
I need to convert the df data.table back into a data.frame. How can I do that?
非常感谢您的帮助。
df <- data.frame(LastPrice = c( 1221, 1220, 1220, 1217, 1216, 1218 , 1216, 1216, 1217, 1220, 1219, 1218, 1220, 1216, 1217, 1218, 1218, 1207, 1206, 1205), KCT = c( 1218, 1218, 1219, 1218, 1221, 1217 , 1217, 1216, 1219, 1216, 1217, 1216, 1219, 1217, 1218, 1217, 1217, 1217, 1219, 1217))
library(data.table)
setDT(df)
df[, check := as.integer(LastPrice > KCT)]
df[, Signal := do.call(pmin, shift(check, 0:2, type="lead"))]
推荐答案
就像 setDT()
通过引用将其输入转换为数据表一样,还有 setDF()
通过引用将其输入转换为数据帧。因此,只需致电
Just like setDT()
converts its input to a data table by reference, there is also setDF()
which converts its input to a data frame by reference. So just call
setDF(df)
,您将返回没有副本的数据框。
and you are back to a data frame with no copies made.
这篇关于将data.table转换为data.frame(即undo setDT)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!