使用列表/数据框/向量将标签分配给带有 Hmisc 的数据框的列名

Using a list / dataframe / vector to assign labels to column names of a dataframe with Hmisc

我想使用数据框的 Variables 列:

  Variables Varcode  Country Ccode  2000  2001 
1 Power     P        France  FR     1213  1234
2 Happiness H        France  FR     1872  2345
3 Power     P        UK      UK     1726  6433
4 Happiness H        UK      UK     2234  9082

将标签分配给另一个(重塑的)数据框的列名(从变量 P 开始):

  Year      Country Ccode P(label=Power) H(label=Happiness)
1 2000      France  FR    1213           1872  
2 2001      France  FR    1234           2345
3 2000      UK      UK    1726           2234
4 2001      UK      UK    6433           9082

我想到了以下几点:

整形前

library(Hmisc)
LabelList <- as.data.frame(df1$Varcode)
LabelList <- as.character(LabelList) #(EDIT)

重塑

df2 %>% 
  select(-Variables) %>% 
  gather(Year, val,`2000`:`2001`) %>% 
  unite(Country_Ccode, Country, Ccode, sep = "_") %>% 
  spread(Varcode, val) %>% 
  separate(Country_Ccode, c("Country", "Ccode"), sep = "_")

整形后(编辑:标签函数只允许矢量为 1)

for(i in LabelList){
label(df2[,i]) <- LabelList[i]

但随后出现以下错误:

Error in `[.data.frame`(List, i) : undefined columns selected
Error : Unsupported index type: factor

在 as.character(LabelList) 之后错误变为:

Error : Column `c(1, 2, 3, 4, 5, 6, .., )

有什么想法吗?

这是一个棘手的问题。所以,我将逐步展示我尝试过的东西。

1。在没有 label<-()

的情况下重塑

在第一次尝试中,我使用了我更熟悉的data.table

library(data.table)   # for melt() and dcast()
library(magrittr)     # for piping %>%
df1 %>% 
  setDT() %>%
  melt(measure.vars = patterns("^20"), variable.name = "Year") %>% 
  dcast(... ~ Varcode + Variables)
   Country Ccode Year H_Happiness P_Power
1:  France    FR 2000        1872    1213
2:  France    FR 2001        2345    1234
3:      UK    UK 2000        2234    1726
4:      UK    UK 2001        9082    6433

现在,值变量的 headers 列包含 VarcodeVariables。我试过这个,因为我不确定 OP 使用 Hmisc::label().

的目的是什么

2。用 label<-()

重塑
df2 <- df1 %>% 
  setDT() %>%
  melt(measure.vars = patterns("^20"), variable.name = "Year") %>% 
  dcast(Year + Country + Ccode ~ Varcode)
   Year Country Ccode    H    P
1: 2000  France    FR 1872 1213
2: 2000      UK    UK 2234 1726
3: 2001  France    FR 2345 1234
4: 2001      UK    UK 9082 6433

现在,我们必须向 HP 列添加标签。

# create list of labels
Lbl <- df1[, .(Variables, Varcode)] %>% unique()
Lbl
   Variables Varcode
1:     Power       P
2: Happiness       H
# set labels
for (i in seq_len(nrow(Lbl))) {
  Hmisc::label(df2[[Lbl$Varcode[i]]]) <- Lbl$Variables[i]
}
str(df2)
Classes ‘data.table’ and 'data.frame':    4 obs. of  5 variables:
 $ Year   : Factor w/ 2 levels "2000","2001": 1 1 2 2
 $ Country: chr  "France" "UK" "France" "UK"
 $ Ccode  : chr  "FR" "UK" "FR" "UK"
 $ H      : 'labelled' int  1872 2234 2345 9082
  ..- attr(*, "label")= chr "Happiness"
 $ P      : 'labelled' int  1213 1726 1234 6433
  ..- attr(*, "label")= chr "Power"
 - attr(*, ".internal.selfref")=<externalptr> 
 - attr(*, "sorted")= chr  "Year" "Country" "Ccode"

现在,HP 两列都已相应标记。

3。完成 OP 的方法

library(dplyr)
library(tidyr)
df2 <- df1 %>% 
  select(-Variables) %>% 
  gather(Year, val,`2000`:`2001`) %>% 
  spread(Varcode, val)
df2
  Country Ccode Year    H    P
1  France    FR 2000 1872 1213
2  France    FR 2001 2345 1234
3      UK    UK 2000 2234 1726
4      UK    UK 2001 9082 6433

请注意,已跳过对 unite()separate() 的调用,因为它们不需要重现预期结果。

Lbl <- df1 %>% 
  distinct(Varcode, Variables)
for (i in seq_len(nrow(Lbl))) {
  Hmisc::label(df2[[Lbl$Varcode[i]]]) <- Lbl$Variables[i]
}
str(df2)
'data.frame': 4 obs. of  5 variables:
 $ Country: chr  "France" "France" "UK" "UK"
 $ Ccode  : chr  "FR" "FR" "UK" "UK"
 $ Year   : chr  "2000" "2001" "2000" "2001"
 $ H      : 'labelled' int  1872 2345 2234 9082
  ..- attr(*, "label")= chr "Happiness"
 $ P      : 'labelled' int  1213 1234 1726 6433
  ..- attr(*, "label")= chr "Power"

数据

df1 <- data.table::fread(
"i  Variables Varcode  Country Ccode  2000  2001 
1 Power     P        France  FR     1213  1234
2 Happiness H        France  FR     1872  2345
3 Power     P        UK      UK     1726  6433
4 Happiness H        UK      UK     2234  9082
", drop = 1L, data.table = FALSE)