Home > Blockchain >  Create another column and add a label to it
Create another column and add a label to it

Time:10-01

I am working with SPSS files where each column name (variable) has a label.

I want to create another column using mutate and add a label to keep it consistent with other columns.

Below is an example - add two column sum (a b c) and mean. Add label a label "Sum of all", "Mean 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 columns sum and mean. Add label a label "Sum of all" , "Mean of all"

CodePudding user response:

Based on the data showed, we get the rowSums by selecting across all the columns (everything()) in the data to create the 'Sum', then do the same except the Sum (-Sum) to get the row wise mean (rowMeans) and use set_label to create a named vector by concatenating the already existing labels (get_label) from the data with the new labels

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

-checking

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