Tsvetan Nikolov Tsvetan Nikolov - 2 months ago 6x
R Question

R - Removing the first and last character of a every factor in a data.table

I'm new to R and I have the following quick question:
What is the best way to delete the first and the last character of each "cell" in a data.table. I have imported the data from a .txt file in which the text has three-character separator - "^|^"?

DT <- fread("file.txt", header = T, sep= "|")

Row Conc group
^1^ ^2.5^ ^A^
^2^ ^3.0^ ^A^
^3^ ^4.6^ ^B^
^4^ ^5.0^ ^B^
^5^ ^3.2^ ^C^
^6^ ^4.2^ ^C^
^7^ ^5.3^ ^D^
^8^ ^3.4^ ^D^

I am able to remove the "^"s column by column using the

DT[, Row := stri_sub(Row,2,-2)]

It converts it to char, but that should be alright.
However, as the data.table I am using has 46 columns, I am looking for more time-efficient way to do it.


Or to continue your approach:


cols <- names(df)
setDT(df)[, (cols) := lapply(.SD, function(x) stri_sub(x, 2, -2))]

To learn more about .SD, refer to the data.table documentation.