This question is tagged with data.table
and fread
but has attracted a base R answer. Therefore, I felt obliged to post a data.table
solution.
The OP wants to add a particular column at a specific place inside a data.table
where this column appears to be missing. The OP seems to expect that the select
parameter to fread()
could be used for that purpose but fread()
does print a
Warning message:
In fread("col1 col2 col3 \na b c\nd e f", :
Column name 'missing' not found in column name header (case sensitive), skipping.
So the missing column has to be added afterwards:
library(data.table)
# add column by reference
DT[, missing := NA][]
col1 col2 col3 missing
1: a b c NA
2: d e f NA
# rearrange column order
setcolorder(DT, c("col1", "col2", "missing", "col3"))[]
col1 col2 missing col3
1: a b NA c
2: d e NA f
Note that data.table
excutes both operations by reference, i.e., it is not required to copy the whole object in order to add a single column or to change an attribute.
Data
library(data.table)
DT <- fread(
"col1 col2 col3
a b c
d e f",
select = c("col1", "col2", "missing", "col3"))