0

I have a delimited text file that looks like this:

df1 <- data.frame(
ID = c(111, 121, 113, 211),
NAME = c('Jim', 'Jon', 'Jan', 'Jack'),
CAT   = c('A', 'A', 'B','A'))

After reading into R with read.delim it looks like this:

df2 <- data.frame( 
ID  = c('Jim', 'Jon', 'Jan', 'Jack'),
NAME   = c('A', 'A', 'B','A'),
CAT   = c('', '', '',''))

This resulting df takes the first delimited column and adds it as the Row Header, so rows show as 111, 121, 113, 211. The columns are then all off by one and the last column is empty.

I can get it to work if I change the argument header = FALSE, all of the columns align correctly, the Row Headers take correct values, but there are no Column Headers anymore.

Code:

df1 <- read.delim(file = str.vec2, header = TRUE, sep = "|", 
                      fill = TRUE)

Txt:

https://pastebin.com/SFb0w0Mm
Jorge
  • 336
  • 1
  • 4
  • 15
  • 3
    Please can you make this problem reproducible, by providing the contents of the **text file** (either as a link or copy-pasted) and the exact code used to read it. – neilfws Aug 03 '22 at 01:19
  • 3
    That pastebin data has 70 `|`s on the first row and 71 `|`s on the second row. I don't know all of `read.delim`'s internals, but my guess is that it'd be easy to infer the first column of rows 2 and beyond are being interpreted as a row name. – r2evans Aug 03 '22 at 01:32

0 Answers0