Skip to content
Prev 380727 / 398498 Next

reading in csv files, some of which have column names and some of which don't

Like Bert, I can't see an easy approach for datasets that have
character rather than numeric data. But here's a simple approach for
distinguishing files that have possible character headers but numeric
data.



readheader <- function(filename) {

possibleheader <- read.table(filename, nrows=1, sep=",", header=FALSE)

if(all(is.numeric(possibleheader[,1]))) {
# no header
infile <- read.table(filename, sep=",", header=FALSE)
} else {
# has header
infile <- read.table(filename, sep=",", header=TRUE)
}

infile
}



#### file noheader.csv ####

1,1,1
2,2,2
3,3,3


#### file hasheader.csv ####

a,b,c
1,1,1
2,2,2
3,3,3

########################
V1 V2 V3
1  1  1  1
2  2  2  2
3  3  3  3
a b c
1 1 1 1
2 2 2 2
3 3 3 3

Sarah
On Tue, Aug 13, 2019 at 2:00 PM Christopher W Ryan <cryan at binghamton.edu> wrote: