Coder Social home page Coder Social logo

Comments (1)

JDenn0514 avatar JDenn0514 commented on August 17, 2024

I believe that I have found a simple solution. I am not sure how easy it would be to add this into pivot_longer as it exists right now but this was the function I created:

pivot_longer_values <- function(data, cols, names_to, values_to, add_value_labels = TRUE) {
  long <- data %>% 
    tidyr::pivot_longer(
      cols = {{ cols }},
      names_to = names_to,
      values_to = values_to
    )
  
  if (add_value_labels == TRUE) {
    # create a vector containing the variable labels
    var_labs <- labelled::var_label(x = data %>% select( {{ cols }})) %>% 
      unlist()
    
    # flip the names and values of the vector
    var_labs <- setNames(names(var_labs), var_labs)
    
    # add the vector of labels as value labels to the new column of names
    labelled::val_labels(long[{{names_to}}]) <- var_labs
    
    return(long)
  } else {
    return(long)
  }
  
}

This function is basically a wrapper around the pivot_longer function with the same function variable names. I also added in add_value_labels so that people have the option of including the variable labels as value labels in the new columns specified under names_to. I tested this with the original data set I created and on other data I have and it seems to work.

# use original data set and make it longer with the value labels included
tbl_long <- tbl %>% 
  pivot_longer_values(
    cols = -c(q1, q3),
    names_to = "var",
    values_to = "resp",
    add_value_labels = TRUE
  )

# use labelled::look_for() to see if it worked
labelled::look_for(tbl_long)

# make a new column with the labels as the names
tbl_long %>% mutate(var_f = as_factor(var))

# here is a more in-depth example of a problem that this solves.
tbl_long %>% 
  # get the labels in a new column
  mutate(var_f = as_factor(var)) %>% 
  # filter out some of the variables
  filter(!var %in% c("q2_6", "q2_7", "q2_8")) %>% 
  # group it by the labels
  group_by(var_f) %>% 
  # get the frequency 
  count(q1)

Hopefully this is something that can be added into tidyr.

from tidyr.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.