英文:
extract variable label from a dataframe to assign to a variable in another dataframe
问题
我有一个数据框(df_old),其中包含一个变量标签,我想提取这些标签并将其放入另一个数据框(df_new)的相应变量中。
在一个循环中,我想使用以下代码,但它不起作用:
attr(df_new[, j], "label") <- attr(df_old[, j], "label")
这不起作用,所以我通过以下方式解决了这个问题:
Tmp <- data.frame(df_old[, j])
names(Tmp) <- "Var"
attr(df_new[, j], "label") <- attr(Tmp$Var, "label")
有没有一种方法可以一次完成?
更新:包括示例df_old和df_new
df_old <- structure(list(A1_1 = structure(c(1, 3, 2, 1, 6, 1, 5, 7, 1, 1), label = "Use Facebook", format.spss = "F1.0", display_width = 1L, labels = c('Every day' = 1, 'A few times a week' = 2, 'A few times a month' = 3, 'Once a month' = 4, 'Every few months' = 5, 'Less often' = 6, 'I never do this activity' = 7), class = c("haven_labelled", "vctrs_vctr", "double")), A1_2 = structure(c(1, 4, 1, 7, 7, 1, 1, 7, 1, 6), label = "Use Instagram", format.spss = "F1.0", display_width = 1L, labels = c('Every day' = 1, 'A few times a week' = 2, 'A few times a month' = 3, 'Once a month' = 4, 'Every few months' = 5, 'Less often' = 6, 'I never do this activity' = 7), class = c("haven_labelled", "vctrs_vctr", "double")), A1_3 = structure(c(1, 1, 2, 7, 7, 6, 1, 7, 2, 1), label = "Use TikTok", format.spss = "F1.0", display_width = 1L, labels = c('Every day' = 1, 'A few times a week' = 2, 'A few times a month' = 3, 'Once a month' = 4, 'Every few months' = 5, 'Less often' = 6, 'I never do this activity' = 7), class = c("haven_labelled", "vctrs_vctr", "double"))), row.names = c(NA, -10L), class = c("tbl_df", "tbl", "data.frame"), label = "TvTPQ2_0.sav")
df_new <- data.frame(matrix(0, ncol = ncol(df_old), nrow = nrow(df_old)))
names(df_new) <- paste0(names(df_old), "_new")
英文:
I have a dataframe (df_old), that has a variable label and I want to extract the labels and put it into respective variables in another dataframe (df_new).
In a loop, I'd like to use this code but it doesn't work
attr( df_new[ , j] , "label") <- attr( df_old[ , j] , "label")
That doesn't work, so I've got around it doing it this way
Tmp <- data.frame( df_old[ , j ] )
names(Tmp) <- "Var"
attr( df_new[ , j] , "label") <- attr( Tmp$Var , "label")
Is there a way to do it in one step?
UPDATE : include example df_old and df_new
df_old <- structure(list(A1_1 = structure(c(1, 3, 2, 1, 6, 1, 5, 7, 1,
1), label = "Use Facebook", format.spss = "F1.0", display_width = 1L, labels = c(`Every day` = 1,
`A few times a week` = 2, `A few times a month` = 3, `Once a month` = 4,
`Every few months` = 5, `Less often` = 6, `I never do this activity` = 7
), class = c("haven_labelled", "vctrs_vctr", "double")), A1_2 = structure(c(1,
4, 1, 7, 7, 1, 1, 7, 1, 6), label = "Use Instagram", format.spss = "F1.0", display_width = 1L, labels = c(`Every day` = 1,
`A few times a week` = 2, `A few times a month` = 3, `Once a month` = 4,
`Every few months` = 5, `Less often` = 6, `I never do this activity` = 7
), class = c("haven_labelled", "vctrs_vctr", "double")), A1_3 = structure(c(1,
1, 2, 7, 7, 6, 1, 7, 2, 1), label = "Use TikTok", format.spss = "F1.0", display_width = 1L, labels = c(`Every day` = 1,
`A few times a week` = 2, `A few times a month` = 3, `Once a month` = 4,
`Every few months` = 5, `Less often` = 6, `I never do this activity` = 7
), class = c("haven_labelled", "vctrs_vctr", "double"))), row.names = c(NA,
-10L), class = c("tbl_df", "tbl", "data.frame"), label = "TvTPQ2_0.sav")
df_new <- data.frame(matrix(0, ncol = ncol(df_old), nrow = nrow(df_old) ))
names(df_new) <- paste0( names(df_old) ,"_new")
答案1
得分: 3
你可以使用`for`循环或任何apply函数系列中的函数来传输属性。
df_new[] <- Map(function(x, y) {attr(x, "label") <- attr(y, "label"); x}, df_new, df_old)
检查属性 -
sapply(df_old, attr, 'label')
A1_1 A1_2 A1_3
"Use Facebook" "Use Instagram" "Use TikTok"
sapply(df_new, attr, 'label')
A1_1_new A1_2_new A1_3_new
"Use Facebook" "Use Instagram" "Use TikTok"
<details>
<summary>英文:</summary>
You may use a `for` loop or any of the apply family of functions to transfer the attributes.
df_new[] <- Map(function(x, y) {attr(x,"label") <- attr(y,"label");x}, df_new, df_old)
check the attributes -
sapply(df_old, attr, 'label')
A1_1 A1_2 A1_3
"Use Facebook" "Use Instagram" "Use TikTok"
sapply(df_new, attr, 'label')
A1_1_new A1_2_new A1_3_new
"Use Facebook" "Use Instagram" "Use TikTok"
</details>
# 答案2
**得分**: 2
你可以使用 `labelled::var_label()` 来提取和分配标签:
library(haven)
library(labelled)
var_label(df_new) <- unlist(var_label(df_old))
attr(df_new$A1_1_new, "label")
"Use Facebook"
<details>
<summary>英文:</summary>
You can use `labelled::var_label()` to both extract and assign the labels:
library(haven)
library(labelled)
var_label(df_new) <- unlist(var_label(df_old))
attr(df_new$A1_1_new, "label")
"Use Facebook"
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论