从一个数据框中提取变量标签,然后分配给另一个数据框中的变量。

huangapple go评论76阅读模式
英文:

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] , &quot;label&quot;) &lt;- attr( df_old[ , j] , &quot;label&quot;) 

That doesn't work, so I've got around it doing it this way

Tmp &lt;- data.frame( df_old[ , j ] )
names(Tmp) &lt;- &quot;Var&quot;
attr(  df_new[ , j] , &quot;label&quot;) &lt;- attr( Tmp$Var , &quot;label&quot;) 

Is there a way to do it in one step?

UPDATE : include example df_old and df_new

df_old &lt;- structure(list(A1_1 = structure(c(1, 3, 2, 1, 6, 1, 5, 7, 1, 
                                  1), label = &quot;Use Facebook&quot;, format.spss = &quot;F1.0&quot;, 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(&quot;haven_labelled&quot;, &quot;vctrs_vctr&quot;, &quot;double&quot;)), A1_2 = structure(c(1, 
                                                                                                              4, 1, 7, 7, 1, 1, 7, 1, 6), label = &quot;Use Instagram&quot;, format.spss = &quot;F1.0&quot;, 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(&quot;haven_labelled&quot;, &quot;vctrs_vctr&quot;, &quot;double&quot;)), A1_3 = structure(c(1, 
                                                                                                                                                                                          1, 2, 7, 7, 6, 1, 7, 2, 1), label = &quot;Use TikTok&quot;, format.spss = &quot;F1.0&quot;, 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(&quot;haven_labelled&quot;, &quot;vctrs_vctr&quot;, &quot;double&quot;))), row.names = c(NA, 
                                                                                                                                                                                                                                                                  -10L), class = c(&quot;tbl_df&quot;, &quot;tbl&quot;, &quot;data.frame&quot;), label = &quot;TvTPQ2_0.sav&quot;)

df_new &lt;- data.frame(matrix(0, ncol = ncol(df_old), nrow = nrow(df_old)  )) 
names(df_new) &lt;-  paste0( names(df_old) ,&quot;_new&quot;)

答案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>



huangapple
  • 本文由 发表于 2023年3月4日 09:32:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/75633142.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定