变量在r中施加突变()函数后丢失标签

发布于 2025-02-13 15:29:41 字数 947 浏览 3 评论 0原文

因此,假设我的一些DF列有标签。

library(Hmisc)
df1 <- data.frame(a = c(0,1,2), b = c(0,1,2), d = c(0,1,2), e = c(0,1,2),
                  f= c("m","f","o"), output = c(0,1,2))
var_labs <- c(a = "aaa",
              b = "bbb",
              #d = "ddd",
              #e = "eee",
              f = "fff",
              output = "ooo")
label(df1) <- as.list(var_labs[match(names(df1), names(var_labs))])

当我应用mutate()在任何列上函数时,该列会丢失其 标签。

library(dplyr)
df2 <- df1 %>%
   mutate_if(is.character,
             .funs = as.factor)

突变后有没有办法保留标签?

so assume some of my df columns have labels.

library(Hmisc)
df1 <- data.frame(a = c(0,1,2), b = c(0,1,2), d = c(0,1,2), e = c(0,1,2),
                  f= c("m","f","o"), output = c(0,1,2))
var_labs <- c(a = "aaa",
              b = "bbb",
              #d = "ddd",
              #e = "eee",
              f = "fff",
              output = "ooo")
label(df1) <- as.list(var_labs[match(names(df1), names(var_labs))])

enter image description here

When I apply mutate() functions on any column, that column loses its label.

library(dplyr)
df2 <- df1 %>%
   mutate_if(is.character,
             .funs = as.factor)

enter image description here

Is there a way to keep the labels after mutating?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

花期渐远 2025-02-20 15:29:41

hmisc标签功能函数授予数据中的各个向量。帧,而不是data.frame本身。因此,问题在于as.factor()函数从列中删除了标签。您可以添加一个保留标签的版本,

as.factor.labelled <- function(x) {
  r <- as.factor(as.character(x))
  label(r)<-label(x)
  r
}

然后突变应保留这些标签,因为此新版本的as.factor将保留标签。

The Hmisc labels functions annotate the individual vectors in the data.frame, not the data.frame itself. So the problem is that the as.factor() function stips off the label from the column. You could add a version that preserves the labels

as.factor.labelled <- function(x) {
  r <- as.factor(as.character(x))
  label(r)<-label(x)
  r
}

Then the mutate should keep those labels because this new version of as.factor will keep the labels.

青萝楚歌 2025-02-20 15:29:41

您可以首先保存df1的标签,然后再次将标签分配给匹配 in Columns to df2像这样:

labs <- Hmisc::label(df1)
library(dplyr)
df2 <- df1 %>%
  mutate_if(is.character,
            .funs = as.factor)

label(df2) <- as.list(labs[match(names(df2), names(labs))])
str(df2)

输出:output:utput:

'data.frame':   3 obs. of  6 variables:
 $ a     : 'labelled' num  0 1 2
  ..- attr(*, "label")= chr "aaa"
 $ b     : 'labelled' num  0 1 2
  ..- attr(*, "label")= chr "bbb"
 $ d     : 'labelled' num  0 1 2
  ..- attr(*, "label")= chr NA
 $ e     : 'labelled' num  0 1 2
  ..- attr(*, "label")= chr NA
 $ f     : Factor w/ 3 levels "f","m","o": 2 1 3
  ..- attr(*, "label")= chr "fff"
 $ output: 'labelled' num  0 1 2
  ..- attr(*, "label")= chr "ooo"

查看(df2)

“在此处输入图像说明”

You could first save the labels of df1 and after that assign again the labels to matching columns to df2 like this:

labs <- Hmisc::label(df1)
library(dplyr)
df2 <- df1 %>%
  mutate_if(is.character,
            .funs = as.factor)

label(df2) <- as.list(labs[match(names(df2), names(labs))])
str(df2)

Output:

'data.frame':   3 obs. of  6 variables:
 $ a     : 'labelled' num  0 1 2
  ..- attr(*, "label")= chr "aaa"
 $ b     : 'labelled' num  0 1 2
  ..- attr(*, "label")= chr "bbb"
 $ d     : 'labelled' num  0 1 2
  ..- attr(*, "label")= chr NA
 $ e     : 'labelled' num  0 1 2
  ..- attr(*, "label")= chr NA
 $ f     : Factor w/ 3 levels "f","m","o": 2 1 3
  ..- attr(*, "label")= chr "fff"
 $ output: 'labelled' num  0 1 2
  ..- attr(*, "label")= chr "ooo"

view(df2):

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文