R: Assign variable labels of data frame columns

后端 未结 4 1406
青春惊慌失措
青春惊慌失措 2020-12-02 17:22

I am struggling with variable labels of data.frame columns. Say I have the following data frame (part of much larger data frame):

data <- data.frame(age =         


        
4条回答
  •  鱼传尺愫
    2020-12-02 17:59

    I highly recommend to use the Hmisc::upData() function.

    Here a reprex example:


    set.seed(22)
    data <- data.frame(age = floor(rnorm(6,25,10)), 
                       sex = gl(2,1,6, labels = c("f","m")))
    var.labels <- c(age = "Age in Years", 
                    sex = "Sex of the participant")
    dplyr::as.tbl(data) # as tibble ---------------------------------------------
    #> # A tibble: 6 × 2
    #>     age    sex
    #>    
    #> 1    19      f
    #> 2    49      m
    #> 3    35      f
    #> 4    27      m
    #> 5    22      f
    #> 6    43      m
    data <- Hmisc::upData(data, labels = var.labels) # update data --------------
    #> Input object size:    1328 bytes;     2 variables     6 observations
    #> New object size: 2096 bytes; 2 variables 6 observations
    Hmisc::label(data) # check new labels ---------------------------------------
    #>                      age                      sex 
    #>           "Age in Years" "Sex of the participant"
    Hmisc::contents(data) # data dictionary -------------------------------------
    #> 
    #> Data frame:data  6 observations and 2 variables    Maximum # NAs:0
    #> 
    #> 
    #>                     Labels Levels   Class Storage
    #> age           Age in Years        integer integer
    #> sex Sex of the participant      2         integer
    #> 
    #> +--------+------+
    #> |Variable|Levels|
    #> +--------+------+
    #> |   sex  |  f,m |
    #> +--------+------+
    

提交回复
热议问题