Skip to content

Instantly share code, notes, and snippets.

@karawoo
Last active June 29, 2016 20:19
Show Gist options
  • Save karawoo/6e701aea0acc5c52d727db7d67fe19bb to your computer and use it in GitHub Desktop.
Save karawoo/6e701aea0acc5c52d727db7d67fe19bb to your computer and use it in GitHub Desktop.
Reading in CSVs with headers on multiple rows
B0S2 B0S3 B0S4
abundance percent cover height abundance percent cover height abundance percent cover height
5 50 71 8 30 9 45
5 3 47 1 1
# Dealing with multi-row headers
To load data that looks like this:
![CSV image](https://pbs.twimg.com/media/CmIg6rVUYAADNQQ.jpg)
Load the zoo and dplyr packages
```r
library("zoo")
library("dplyr")
```
Then create headers that have all the info from both header rows.
```r
## Read in the first header row and replace blank cells with the most recent
## non-blank value using na.locf()
head1 <- read.csv("data.csv", nrow = 1, header = FALSE,
stringsAsFactors = FALSE, colClasses=rep("character", 9),
na.strings = "") %>%
as.character() %>%
na.locf()
## Read in second header row
head2 <- read.csv("data.csv", skip = 1, nrow = 1, header = FALSE,
stringsAsFactors = FALSE)
## Paste headers together
header <- paste(head1, head2, sep = "-")
header
## [1] "B0S2-abundance" "B0S2-percent cover" "B0S2-height"
## [4] "B0S3-abundance" "B0S3-percent cover" "B0S3-height"
## [7] "B0S4-abundance" "B0S4-percent cover" "B0S4-height"
```
Then load the data without headers and add the combined headers back on.
```
dat <- read.csv("data.csv", skip = 2, stringsAsFactors = FALSE,
header = FALSE) %>%
setNames(header)
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment