简体   繁体   English

如何在R中处理data.table列as.character?

[英]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.charactervector 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]

data 数据

DT <- data.table(Col1 = 1:5, Col2= 6:10, Col3= LETTERS[1:5])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在R中的data.table中使用eval,parse和as.character - using eval, parse and as.character in data.table in R R data.table fread如何处理嵌入式^ @字符? - R data.table fread how to deal with embedded ^@ character? 神秘:为什么我添加和减去另一个变量时,data.table中的as.character()函数运行得更快? - Mystery: Why does the as.character() function in a data.table run faster if I add and subtract another variable? 如何处理带有列表列和 NULL 值的 data.table - How to deal with data.table with list column and NULL values 通过data.table R中的字符变量引用列 - Referring to a column through a character variable in data.table R R-在data.table中过滤字符日期 - R - Filtering character dates in data.table 在 R 中,如何对由两个字符列聚合的 data.table 列中的值求和,其中列名和行名等于字符串 output 的矩阵? - In R how do I sum values in a data.table column aggregated by two character columns, with matrix with colnames and rownames equal to strings output? 在R中,如何按data.table中的特定值对列进行排序? - In R, how to order a column by specific value in data.table? 如何使用R dataframe / data.table展开单个列, - How to unfold a single column with R dataframe/data.table, R:如何将“变量”的子集广播到data.table中的单独列? - R: How to dcast a subset of 'variable' to a separate column in data.table?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM