Survey Tools - Replacing Column Names with Question Labels
Source:R/srv_replace_colnames.R
srv_replace_colnames.RdUses srv_extract_label to replace the names of the
columns with it's label.
Arguments
- df
Original Survey Data Frame. Most likely imported through
haven::read_spss()- exc
Column names or numbers to exclude.
- ...
Further arguments passed to
srv_extract_label()- verbose
Whether to print message with new names of columns.
Examples
df <- mtcars
attr(df$disp, which = "label") <- "Q1 - Question for this variable"
colnames(df)
#> [1] "mpg" "cyl" "disp" "hp" "drat" "wt" "qsec" "vs" "am" "gear"
#> [11] "carb"
df <- srv_replace_colnames(df, exc = c("mpg"), split = TRUE, sep = " - ", position = 2)
#> [1] "column 2(cyl) has no label"
#> [1] "column 4(hp) has no label"
#> [1] "column 5(drat) has no label"
#> [1] "column 6(wt) has no label"
#> [1] "column 7(qsec) has no label"
#> [1] "column 8(vs) has no label"
#> [1] "column 9(am) has no label"
#> [1] "column 10(gear) has no label"
#> [1] "column 11(carb) has no label"
colnames(df)
#> [1] "mpg" "cyl"
#> [3] "Question for this variable" "hp"
#> [5] "drat" "wt"
#> [7] "qsec" "vs"
#> [9] "am" "gear"
#> [11] "carb"