【问题标题】:Create another column and add a label to it创建另一列并向其添加标签
【发布时间】:2022-09-30 23:38:38
【问题描述】:

我正在使用 SPSS 文件,其中每个列名(变量)都有一个标签。

我想使用 mutate 创建另一列并添加一个标签以使其与其他列保持一致。

下面是一个示例 - 添加另一列 sum (a + b + c) 并添加标签标签 \"Sum of all\"

library(tidyverse)
library(sjlabelled)

# Set variable labels for data frame
dummy <- tibble(a = sample(1:4, 10, replace = TRUE),
                    b = sample(1:4, 10, replace = TRUE),
                    c = sample(1:4, 10, replace = TRUE)) %>% 
  set_label(c(\"Variable A\", \"Variable B\", \"Variable C\"))


# add another column sum (a + b + c) and add label a label \"Sum of all\" 

    标签: r tidyverse


    【解决方案1】:

    我们可以做

    library(sjlabelled)
    library(dplyr)
    dummy <- dummy %>%
       mutate(Sum = rowSums(across(everything()))) %>%
        set_label(c(get_label(dummy), "Sum" = "Sum of all" )) 
    

    -检查

    > get_label(dummy)
               a            b            c          Sum 
    "Variable A" "Variable B" "Variable C" "Sum of all" 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-15
      • 1970-01-01
      • 1970-01-01
      • 2020-01-23
      • 2014-01-22
      • 2019-07-13
      相关资源
      最近更新 更多