Comments (1)
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)
- Feature Request: Allow across() for column selection in complete() HOT 4
- value in gather cannot be the same as any of the original dataset variables HOT 1
- `crossing()` adds missing factor levels (either a bug or a documentation issue)
- Upkeep for tidyr (2023)
- Make `separate_longer_*()` and `separate_wider_*()` generics?
- Release tidyr 1.3.1
- separate_wider_delim changes input column names when using names_sep with cols_remove=FALSE HOT 1
- Change in pivot_wider behavior and error messages when using column numbers for `id_cols` HOT 1
- Feature request: function count the missing value
- Feature request: `.vary` in `expand_grid()`
- Solutions for a crowded namespace: selective removal of items? Any better ideas?
- int64 summation fails HOT 3
- Error in tidyr package & issue with columns being added to Markdown output where used to put into rows (no change to code) HOT 10
- keep getting the error while running RStudio R 4.3.3 on MacOS Big Sur HOT 1
- Better error message for empty tidyselect quosure HOT 2
- In `separate_wider_delim()`, could `merge` have an option for `too_many = "merge_end"`
- num_range() with multiple-specified prefixes gives alternating result HOT 1
- `slice()` behaves differently to `head()` on nested tibbles vs. unnested tibbles. HOT 2
- unnest_longer with multiple cols - values_to=glue::glue({col}_long) not working
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from tidyr.