Issue reading csv with variable that has a numeral sign in the header [r]

611 views Asked by At

I have a similar issue to this past question: How can read 'Numeral Signs-#' as part of a column header?

I too have a pound/hash/number sign as part of the name of the first variable in a csv file: "Seg#"

When I read in the csv using readr's "read_csv", I see a warning message about parsing failures, where it says: expected 25 columns, actual 26 columns. It reads in the data, but each column name is shifted to the right, and the first column of data (what was previously under the variable "Seg#") is now missing.

Using read.csv produces the same data frame but without any warnings.

I attempted to solve this using the recommendation in the question linked above:

d1 <- read.csv('11104.wav.csv', comment.char = "", header=T, check.names = FALSE)

...But it simply did the same thing: deleted the first column of data and shifted all names once to the right.

When I attempt "read.table" instead of read.csv, I see the following error message:

Error in read.table("11104.wav.csv", comment.char = "", header = T, check.names = FALSE) : more columns than column names

Any help would be deeply appreciated!


EDIT: Including data example

Here is how the data look in Excel:

Seg#     Start Pos (Sec.)    End Pos (Sec.)  Energy
1   4.96    5.98    2
2   5.98    6.98    4
3   6.98    7.98    5
4   7.98    8.68    8
5   12.02   13.04   3
6   13.04   14.04   2
7   14.04   14.76   3

When read into R, however, the data looks like this:

Seg#    Start Pos (Sec.)    Pos (Sec.)  Energy
4.96    5.98    2   NA
5.98    6.98    4   NA
6.98    7.98    5   NA
7.98    8.68    8   NA
12.02   13.04   3   NA
13.04   14.04   2   NA
14.04   14.76   3   NA
1

There are 1 answers

0
Oliver Frost On

Try reading the files in using the fread() function in the data.table library? I created a similar CSV file with # in the column headers and it loaded in without any issues.

DT <- fread("11104.wav.csv")