Here are two possibilities that also provide some useful flexibility (shown below):
library(unheadr)
mash_colnames(df, n_name_rows = 1, keep_names = FALSE)
#> col1 col2 col3 col4 col5
#> 2 row1 2 4 5 56
#> 3 row2 74 74 3 534
#> 4 row3 865 768 8 7
#> 5 row4 68 86 65 87
library(scrutiny)
row_to_colnames(df)
Both these options allow you to get column names if they are broken across multiple rows, with the unheadr package offering a bit more flexibility:
babies <-
data.frame(
stringsAsFactors = FALSE,
Baby = c(NA, NA, "Angie", "Yean", "Pierre"),
Age = c("in", "months", "11", "9", "7"),
Weight = c("kg", NA, "2", "3", "4"),
Ward = c(NA, NA, "A", "B", "C")
)
babies
#> Baby Age Weight Ward
#> 1 <NA> in kg <NA>
#> 2 <NA> months <NA> <NA>
#> 3 Angie 11 2 A
#> 4 Yean 9 3 B
#> 5 Pierre 7 4 C
mash_colnames(babies, n_name_rows = 2, keep_names = TRUE)
#> Baby Age_in_months Weight_kg Ward
#> 3 Angie 11 2 A
#> 4 Yean 9 3 B
#> 5 Pierre 7 4 C
Or some survey data tend to have user response options in a different row from the question:
survey <-
data.frame(
stringsAsFactors = FALSE,
X1 = c("Participant", NA, "12", "34", "45", "123"),
X2 = c(
"How did you hear about us?",
"TV", "TRUE", "FALSE", "FALSE", "FALSE"
),
X3 = c(NA, "Social Media", "FALSE", "TRUE", "FALSE", "FALSE"),
X4 = c(NA, "Radio", "FALSE", "TRUE", "FALSE", "TRUE"),
X5 = c(NA, "Flyer", "FALSE", "FALSE", "FALSE", "FALSE"),
X6 = c("Age", NA, "31", "23", "19", "24")
)
survey
#> X1 X2 X3 X4 X5 X6
#> 1 Participant How did you hear about us? <NA> <NA> <NA> Age
#> 2 <NA> TV Social Media Radio Flyer <NA>
#> 3 12 TRUE FALSE FALSE FALSE 31
#> 4 34 FALSE TRUE TRUE FALSE 23
#> 5 45 FALSE FALSE FALSE FALSE 19
#> 6 123 FALSE FALSE TRUE FALSE 24
mash_colnames(survey, 2, keep_names = FALSE, sliding_headers = TRUE, sep = "_")
#> Participant How did you hear about us?_TV How did you hear about us?_Social Media How did you hear about us?_Radio How did you hear about us?_Flyer Age
#> 3 12 TRUE FALSE FALSE FALSE 31
#> 4 34 FALSE TRUE TRUE FALSE 23
#> 5 45 FALSE FALSE FALSE FALSE 19
#> 6 123 FALSE FALSE TRUE FALSE 24
rename_allfrom dplyrcolnames(t1) <- t1[1, ]and thent1 <- t1[-1, ]should work