'Reading in a table with non-standard row names in R?

I have a .txt file that looks like this:

       xyz ghj asd qwe
a / b:  1   2   3   4
c / d:  5   6   7   8
e / f:  9   10  11  12
        ...
        ...

I'm trying to use read.table(header = T) but it seems to be misinterpreting the row name. Is there a way to deal with this in read.table() or should I just use readLines()

r


Solution 1:[1]

There is no option to just skip a few characters in each row using a read.table option.

Instead, you can call read.table twice, once for all the data after the first row, and the second time for the header.

Where your data are in a file called "test.txt", you would do:

library(magrittr)
tmp <- read.table(file="test.txt", sep="", stringsAsFactors = FALSE, skip=1)[, -c(1:3)] %>%
         setNames(read.table(file="test.txt", sep="", stringsAsFactors = FALSE, nrows=1))

> tmp
  xyz ghj asd qwe
1   1   2   3   4
2   5   6   7   8
3   9  10  11  12
>

Package magrittr is what gives you the pipe operator %>% that allows you to read the data and the header separately, but put them together in a single line. If you have a sufficiently-new R version you can use the |> operator instead, without the magrittr package.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1