2014-12-07 51 views
14

我正在努力處理data.frame列的變量標籤。說我有一個數據幀,因爲這(更大的數據幀的一部分):R:分配數據幀列的變量標籤

data <- data.frame(age = c(21, 30, 25, 41, 29, 33), sex = factor(c(1, 2, 1, 2, 1, 2), labels = c("Female", "Male"))) 

我也有此數據的變量標籤名爲向量:

var.labels <- c(age = "Age in Years", sex = "Sex of the participant") 

我想要做什麼是使用Hmisc包中的函數labelvar.labels中的變量標籤分配給數據幀data中的列。我可以用一個像這樣做他們一個事後檢查結果:

> label(data[["age"]]) <- "Age in years" 
> label(data[["sex"]]) <- "Sex of the participant" 
> label(data) 
       age      sex 
     "Age in years" "Sex of the participant" 

變量標籤被分配爲列的屬性:

> attr(data[["age"]], "label") 
[1] "Age in years" 
> attr(data[["sex"]], "label") 
[1] "Sex of the participant" 

精彩。但是,對於較大的數據幀,例如100列或更多列,這不會很方便或有效。我可以輕鬆做的另一件事是直接將它們分配爲屬性:

> attr(data, "variable.labels") <- var.labels 

沒有幫助。變量標籤沒有被分配到列:

> label(data) 
age sex 
"" "" 

相反,他們被分配爲數據幀本身的屬性(見列表的最後一個組件):

> attributes(data) 
$names 
[1] "age" "sex" 

$row.names 
[1] 1 2 3 4 5 6 

$class 
[1] "data.frame" 

$variable.labels 
       age      sex 
     "Age in Years" "Sex of the participant" 

這是不是我想要的。我需要變量標籤作爲列的屬性。我試着寫了下面的函數(和許多其他):

set.var.labels <- function(dataframe, label.vector){ 
    column.names <- names(dataframe) 
    dataframe <- mapply(label, column.names, label.vector) 
    return(dataframe) 
} 

而不是執行它:

> set.var.labels(data, var.labels) 

沒有幫助。它返回矢量var.labels的值,但不分配變量標籤。如果我嘗試將它分配給一個新對象,它只包含變量標籤的值作爲一個向量。

回答

15

您可以指定使用lapply標籤:

var.labels = c(age="Age in Years", sex="Sex of the participant") 

label(data) = lapply(names(var.labels), 
        function(x) label(data[,x]) = var.labels[x]) 

label(data) 
        age      sex 
      "Age in Years" "Sex of the participant" 

lapply應用一個函數列表或向量的每個元素。在這種情況下,該函數適用於每個值names(var.labels)。通過閱讀一些教程是獲得總體思路的好方法,但是如果您在不同情況下開始使用lapply並且看看它的行爲如何,那麼您將真正掌握它。

+0

@ eipi10:非常感謝你!有用!這完全是我所需要的。使用'apply'函數族時,我在理解索引時遇到了問題。有沒有我可以閱讀的指南,或者這是一個經驗問題? – panman 2014-12-07 21:39:21

+0

關於'lapply'的簡要教程,[this](http://rollingyours.wordpress.com/category/r-programming-apply-lapply-tapply/)和[this](https://nsaunders.wordpress.com/2010/08/20/a-brief-introduction-to-apply-in-r /)可能會有幫助。我還爲我的答案增加了一些解釋。 – eipi10 2014-12-08 05:34:27

2

如果您的標籤矢量與您的data.frame列的順序相匹配,但不是一個已命名的矢量(因此不能用於在其他答案中按照lapply方法按名稱子集數據.frame列) ,你可以使用for循環:

for(i in seq_along(data)){ 
    Hmisc::label(data[, i]) <- var.labels[i] 
} 

label(data) 
#>      age      sex 
#>   "Age in Years" "Sex of the participant" 
3

我強烈推薦使用Hmisc::upData()函數。

這裏一個reprex例如:


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 
#> <dbl> <fctr> 
#> 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 | 
#> +--------+------+ 
+0

'Hmisc :: upData(data,labels =)'很棒!尋找這個小時。 – 2017-08-08 19:01:42