ljsok
上面的with = FALSE
表示把with参数(这个参数针对于j
)关掉,意思就是不把列名当做变量处理。with
参数默认为TRUE
,此时其作用和base R中的函数with()
作用一样。举例如下:
library(data.table)
csv <- "a,b,c,d\nrow1,1,2,3\nrow2,11,12,13\nrow3,111,112,113"
out <- data.table::fread(csv)
out # DataTable
#> a b c d
#> 1: row1 1 2 3
#> 2: row2 11 12 13
#> 3: row3 111 112 113
put <- data.frame(out)
put # DataFrame
#> a b c d
#> 1 row1 1 2 3
#> 2 row2 11 12 13
#> 3 row3 111 112 113
out[, c] # 列名c是一个变量
#> [1] 2 12 112
out[, 'c', with = FALSE] # 用了把with参数关掉,'c'就是字符串
#> c
#> 1: 2
#> 2: 12
#> 3: 112
# out[, c, with = FALSE] ——> 会报错
with(put, c) # 等价于 out[, c]
#> [1] 2 12 112
put[, 'c'] # 等价于 out[, 'c', with = FALSE]
#> [1] 2 12 112
<sup>Created on 2022-09-17 by the reprex package (v2.0.1)</sup>