How can I read in a data table with the first column name missing?

Viewed 114
2 Answers

import all columns (then change name)

# load package
library(data.table)

# import txt
df <- fread("https://socialsciences.mcmaster.ca/jfox/Books/Applied-Regression-2E/datasets/Cowles.txt")

# change 1st column's name
names(df)[1] <- 'id'

import all except 1st column

df <- fread("https://socialsciences.mcmaster.ca/jfox/Books/Applied-Regression-2E/datasets/Cowles.txt", drop = 1)

We can use setnames to change the first column's name (which is automatically named V1) when you read in the file, and do it all in one step:

library(data.table)

dt <-
  setnames(
    fread(
      "https://socialsciences.mcmaster.ca/jfox/Books/Applied-Regression-2E/datasets/Cowles.txt"
    ),
    "V1",
    "ID"
  )

Output

        ID neuroticism extraversion    sex volunteer
   1:    1          16           13 female        no
   2:    2           8           14   male        no
   3:    3           5           16   male        no
   4:    4           8           20 female        no
   5:    5           9           19   male        no
  ---                                               
1417: 1417           5           10   male       yes
1418: 1418           8            4 female       yes
1419: 1419           8            8   male       yes
1420: 1420          19           20 female       yes
1421: 1421          15           20   male       yes

If you want to ignore the first column, then you can either use drop (as suggested in the other answer), or you can also use select to keep the columns that you want.

dt <- fread(
  "https://socialsciences.mcmaster.ca/jfox/Books/Applied-Regression-2E/datasets/Cowles.txt", select = c(2:5)
)

      neuroticism extraversion    sex volunteer
   1:          16           13 female        no
   2:           8           14   male        no
   3:           5           16   male        no
   4:           8           20 female        no
   5:           9           19   male        no
  ---                                          
1417:           5           10   male       yes
1418:           8            4 female       yes
1419:           8            8   male       yes
1420:          19           20 female       yes
1421:          15           20   male       yes
Related