我想使用数据框的列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, .., )
有什么想法吗?
答案 0 :(得分: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
现在,值vars的列标题包含Varcode
和Variables
。我之所以尝试这样做是因为我不确定OP打算通过使用Hmisc::label()
来实现什么。
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
现在,我们必须将标签添加到列H
和P
中。
# 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"
现在,H
和P
列均已相应标记。
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)