library(labelled)
var_label(iris$Petal.Length) <- "Length of sepal"
var_label(iris) <- list(Petal.Length = "Length of petal",
Petal.Width = "Width of Petal")
var_label(iris$Petal.Width)
## [1] "Width of Petal"
## $Sepal.Length
## NULL
##
## $Sepal.Width
## NULL
##
## $Petal.Length
## [1] "Length of petal"
##
## $Petal.Width
## [1] "Width of Petal"
##
## $Species
## NULL
To remove a variable label, use NULL.
可以通过 look for ()显示和搜索变量名和标签:
创建带标签的向量的第一种方法是使用标签函数。并不是必须为在矢量中观察到的每个值提供一个标签。您还可以为未观察到的值提供标签。
## <labelled<double>[10]>
## [1] 1 2 2 2 3 9 1 3 2 NA
##
## Labels:
## value label
## 1 yes
## 3 no
## 8 don't know
## 9 refused
## yes no don't know refused
## 1 3 8 9
## [1] "don't know"
## <labelled<double>[10]>
## [1] 1 2 2 2 3 9 1 3 2 NA
##
## Labels:
## value label
## 1 yes
## 3 nno
## 5 bug
## <labelled<double>[10]>
## [1] 1 2 2 2 3 9 1 3 2 NA
##
## Labels:
## value label
## 1 yes
## 3 no
## 5 bugs
## <labelled<double>[10]>
## [1] 1 2 2 2 3 9 1 3 2 NA
##
## Labels:
## value label
## 1 yes
## 3 no
## 5 bugs
## 2 maybe
## [1] 1 2 2 2 3 9 1 3 2 NA
## <labelled<double>[10]>
## [1] 1 2 2 2 3 9 1 3 2 NA
##
## Labels:
## value label
## 1 yes
Note that applying val_labels
to a factor will have no effect!!
## [1] 1 2 3
## Levels: 1 2 3
## [1] 1 2 3
## Levels: 1 2 3
You could also apply value labels to several columns of a data frame.
df <- data.frame(v1 = 1:3, v2 = c(2, 3, 1), v3 = 3:1)
val_label(df, 1) <- "yes"
val_label(df[, c("v1", "v3")], 2) <- "maybe"
val_label(df[, c("v2", "v3")], 3) <- "no"
val_labels(df)
## $v1
## yes maybe
## 1 2
##
## $v2
## yes no
## 1 3
##
## $v3
## yes maybe no
## 1 2 3
## [1] 1 2 2 2 3 9 1 3 2 NA
val_label(v,1) <- "yes"
val_label(v,3) <- "no"
val_label(v,9) <- "refused"
val_label(v,2) <- "maybe"
val_label(v,8) <- "don't know"
v
## <labelled<double>[10]>
## [1] 1 2 2 2 3 9 1 3 2 NA
##
## Labels:
## value label
## 1 yes
## 3 no
## 9 refused
## 2 maybe
## 8 don't know
It could be useful to reorder the value labels according to their attached values.
## <labelled<double>[10]>
## [1] 1 2 2 2 3 9 1 3 2 NA
##
## Labels:
## value label
## 1 yes
## 2 maybe
## 3 no
## 8 don't know
## 9 refused
## <labelled<double>[10]>
## [1] 1 2 2 2 3 9 1 3 2 NA
##
## Labels:
## value label
## 9 refused
## 8 don't know
## 3 no
## 2 maybe
## 1 yes
## <labelled<double>[10]>
## [1] 1 2 2 2 3 9 1 3 2 NA
##
## Labels:
## value label
## 8 don't know
## 2 maybe
## 3 no
## 9 refused
## 1 yes
## <labelled<double>[10]>
## [1] 1 2 2 2 3 9 1 3 2 NA
##
## Labels:
## value label
## 1 yes
## 3 no
## 9 don't know
## [1] 100
## <labelled_spss<double>[10]>
## [1] 1 2 2 2 3 9 1 3 2 NA
## Missing values: 100
##
## Labels:
## value label
## 1 yes
## 3 no
## 9 don't know
## [1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE TRUE
## <labelled<double>[10]>
## [1] 1 2 2 2 3 9 1 3 2 NA
##
## Labels:
## value label
## 1 yes
## 3 no
## 8 don't know
## 9 refused
## [1] yes 2 2 2 no refused yes no 2
## [10] <NA>
## Levels: yes 2 no don't know refused
## [1] 1 2 2 2 3 9 1 3 2 <NA>
## Levels: 1 2 3 8 9
## [1] [1] yes [2] 2 [2] 2 [2] 2 [3] no [9] refused
## [7] [1] yes [3] no [2] 2 <NA>
## Levels: [1] yes [2] 2 [3] no [8] don't know [9] refused
## <labelled<double>[10]>
## [1] 1 2 2 2 3 9 1 3 2 NA
##
## Labels:
## value label
## 1 yes
## 3 no
## 8 don't know
## 9 refused
## [1] "yes" "2" "2" "2" "no" "refused" "yes"
## [8] "no" "2" NA
library(tidyverse)
df <- data.frame(
a = labelled(c(1, 1, 2, 3), labels = c(No = 1, Yes = 2)),
b = labelled(c(1, 1, 2, 3), labels = c(No = 1, Yes = 2, DK = 3)),
c = labelled(c(1, 1, 2, 2), labels = c(No = 1, Yes = 2, DK = 3)),
d = labelled(c("a", "a", "b", "c"), labels = c(No = "a", Yes = "b")),
e = labelled_spss(
c(1, 9, 1, 2),
labels = c(No = 1, Yes = 2),
na_values = 9
)
)
df %>% glimpse()
## Rows: 4
## Columns: 5
## $ a <dbl+lbl> 1, 1, 2, 3
## $ b <dbl+lbl> 1, 1, 2, 3
## $ c <dbl+lbl> 1, 1, 2, 2
## $ d <chr+lbl> "a", "a", "b", "c"
## $ e <dbl+lbl> 1, 9, 1, 2
## Rows: 2,000
## Columns: 17
## $ id_woman <dbl> 391, 1643, 85, 881, 1981, 1072, 1978, 1607, 738, ...
## $ id_household <dbl> 381, 1515, 85, 844, 1797, 1015, 1794, 1486, 711, ...
## $ weight <dbl> 1.803150, 1.803150, 1.803150, 1.803150, 1.803150,...
## $ interview_date <date> 2012-05-05, 2012-01-23, 2012-01-21, 2012-01-06, ...
## $ date_of_birth <date> 1997-03-07, 1982-01-06, 1979-01-01, 1968-03-29, ...
## $ age <dbl> 15, 30, 33, 43, 25, 18, 45, 23, 49, 31, 26, 45, 2...
## $ residency <dbl+lbl> 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, ...
## $ region <dbl+lbl> 4, 4, 4, 4, 4, 3, 3, 3, 3, 3, 3, 3, 2, 2, 2, ...
## $ instruction <dbl+lbl> 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 2, 1, ...
## $ employed <dbl+lbl> 1, 1, 0, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, ...
## $ matri <dbl+lbl> 0, 2, 2, 2, 1, 0, 1, 1, 2, 5, 2, 3, 0, 2, 1, ...
## $ religion <dbl+lbl> 1, 3, 2, 3, 2, 2, 3, 1, 3, 3, 2, 3, 2, 2, 2, ...
## $ newspaper <dbl+lbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, ...
## $ radio <dbl+lbl> 0, 1, 1, 0, 0, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, ...
## $ tv <dbl+lbl> 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, ...
## $ ideal_nb_children <dbl+lbl> 4, 4, 4, 4, 4, 5, 10, 5, 4, 5, 6, 1...
## $ test <dbl+lbl> 0, 9, 0, 0, 1, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, ...
## <labelled<double>[10]>: Urban / rural residency
## [1] 1 2 2 2 2 1 2 2 1 2
##
## Labels:
## value label
## 1 urban
## 2 rural
## Rows: 2,000
## Columns: 17
## $ id_woman <dbl> 391, 1643, 85, 881, 1981, 1072, 1978, 1607, 738, ...
## $ id_household <dbl> 381, 1515, 85, 844, 1797, 1015, 1794, 1486, 711, ...
## $ weight <dbl> 1.803150, 1.803150, 1.803150, 1.803150, 1.803150,...
## $ interview_date <date> 2012-05-05, 2012-01-23, 2012-01-21, 2012-01-06, ...
## $ date_of_birth <date> 1997-03-07, 1982-01-06, 1979-01-01, 1968-03-29, ...
## $ age <dbl> 15, 30, 33, 43, 25, 18, 45, 23, 49, 31, 26, 45, 2...
## $ residency <fct> rural, rural, rural, rural, rural, rural, rural, ...
## $ region <fct> West, West, West, West, West, South, South, South...
## $ instruction <fct> none, none, none, none, primary, none, none, none...
## $ employed <fct> yes, yes, no, yes, yes, no, yes, no, yes, yes, ye...
## $ matri <fct> single, living together, living together, living ...
## $ religion <fct> Muslim, Protestant, Christian, Protestant, Christ...
## $ newspaper <fct> no, no, no, no, no, no, no, no, no, no, no, no, n...
## $ radio <fct> no, yes, yes, no, no, yes, yes, no, no, no, yes, ...
## $ tv <fct> no, no, no, no, no, yes, no, no, no, no, yes, yes...
## $ ideal_nb_children <fct> 4, 4, 4, 4, 4, 5, 10, 5, 4, 5, 6, 10, 2, 6, 6, 6,...
## $ test <fct> no, missing, no, no, yes, no, no, no, no, yes, ye...