[英]how to deal with data.table column as.character in R?
I'm trying to use data.table rather data.frame(for a faster code). 我正在尝试使用data.table而不是data.frame(以获得更快的代码)。 Despite the syntax difference between than, I'm having problems when I need to extract a specific character column and use it as character vector.
尽管它们之间在语法上有所不同,但是当我需要提取特定的字符列并将其用作字符向量时遇到了问题。 When I call:
当我打电话时:
library(data.table)
DT <- fread("file.txt")
vect <- as.character(DT[, 1, with = FALSE])
class(vect)
###[1] "character"
head(vect)
It returns: 它返回:
[1] "c(\"uc003hzj.4\", \"uc021ofx.1\", \"uc021olu.1\", \"uc021ome.1\", \"uc021oov.1\", \"uc021opl.1\", \"uc021osl.1\", \"uc021ovd.1\", \"uc021ovp.1\", \"uc021pdq.1\", \"uc021pdv.1\", \"uc021pdw.1\")
Any ideas of how to avoid these "\\" in the output? 关于如何在输出中避免出现这些“ \\”的想法?
The as.character
works on vector
s and not on data.frame/data.table
objects in the way the OP expected. as.character
对vector
s起作用,而不对OP期望的方式作用于data.frame/data.table
对象。 So, if we need to get the first column as character
class, subset with .SD[[1L]]
and apply the as.character
因此,如果需要将第一列作为
character
类,请使用.SD[[1L]]
子集并应用as.character
DT[, as.character(.SD[[1L]])]
If there are multiple columns, we can specify the column index with .SDcols
and loop over the .SD
to convert to character
and assign ( :=
) the output back to the particular columns. 如果有多个列,我们可以使用
.SDcols
指定列索引,并在.SD
上循环以转换为character
然后将( :=
)输出分配回特定的列。
DT[, (1:2) := lapply(.SD, as.character), .SDcols= 1:2]
DT <- data.table(Col1 = 1:5, Col2= 6:10, Col3= LETTERS[1:5])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.