R: Assign variable labels of data frame columns

后端 未结 4 1402
青春惊慌失措
青春惊慌失措 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:49

    If your vector of labels matches the order of your data.frame columns, but isn't a named vector (so can't be used to subset data.frame columns by name like the lapply approach in the other answer), you can use a for-loop:

    for(i in seq_along(data)){
      Hmisc::label(data[, i]) <- var.labels[i]
    }
    
    label(data)
    #>                      age                      sex 
    #>           "Age in Years" "Sex of the participant"
    

提交回复
热议问题