使用 data.table 构建()

有一个同名的构造函数:

DT <- data.table(
  x = letters[1:5], 
  y = 1:5, 
  z = (1:5) > 3
)
#    x y     z
# 1: a 1 FALSE
# 2: b 2 FALSE
# 3: c 3 FALSE
# 4: d 4  TRUE
# 5: e 5  TRUE

data.frame 不同,data.table 默认情况下不会将字符串强制转换为因子:

sapply(DT, class)
#               x           y           z 
#     "character"   "integer"   "logical"