I'm new to data.table and I'm trying to learn it and try to move from data.frame to data.table.
Now, I'm trying to split text into new columns and I'm following the discussion here.
This is what I'm trying to do.
This is an example data:
# sample data frame
test <- data.table(POS = c(254, 280, 303, 22, 105, 173, 230, 235, 257, 258),
value = c("0/1:15:3:123:12:478:-38.8484,0,-6.94934",
"0/0:15:15:577:0:0:0,-4.51545,-52.25",
"0/0:13:13:276:0:0:0,-3.91339,-25.0455",
"0/0:367:347:13643:0:0:0,-104.457,-1226.73",
"0/0:367:344:13145:5,0,1,0:168,0,41,0:0,-89.9158,-1166.99,-103.554,-1168.49,-1182.1,-100.161,-1165.11,-1178.71,-1178.41,-103.554,-1168.49,-1182.1,-1178.71,-1182.1",
"0/1:344:180:5411:156:4394:-294.227,0,-385.695",
"0/0:352:349:12289:1:12:0,-104.28,-1104.15",
"0/0:352:345:10691:1:12:0,-103.081,-960.583",
"0/0:352:351:13162:1:41:0,-101.868,-1179.6",
"0/0:352:349:12593:0:0:0,-105.059,-1132.45"))
I wanted to split the value into different columns using ":" with specific column names. The code below (which I learned from the link above) do that one perfectly.
test[, c("GT", "DP", "RO", "QR", "AO", "QA", "GL") := tstrsplit(value, ":",
fixed=TRUE)]
However, is it possible to use an R object in place of the c(names) above? Like this:
# new column names
namesForm <- c("GT", "DP", "RO", "QR", "AO", "QA", "GL")
Then, use that namesForm like below:
# use the namesForm as column names
test[, namesForm := tstrsplit(value, ":", fixed=TRUE)]
This gives me warning and a different output (gives me a data.table of 3 variables; the last one a list of 10, did a recycling of the 7 list from the tstrsplit output)
Warning message:
In `[.data.table`(test, , `:=`(namesForm, tstrsplit(value, ":", :
Supplied 7 items to be assigned to 10 items of column 'namesForm' (recycled leaving remainder of 3 items).
So my question again is, is it possible to use an R object/variable in place of an explicit c()?