To widen them in the way we want, we will need to do a bit more work. R spreading multiple columns with tidyr. 1 1 9 8 6 5. If TRUE, type.convert() with asis = TRUE will be run on each of the new columns. One way to do this, just to show what’s needed, is using the As you can see, we stratify by sex, race, and stratum, and then in the new columns we have average income values and observation counts for BA and HS values of education.There the education variable is still tidily organized, and so the mean income and count variables are their own columns, rather than widened out.

NA), and implicit missings, rows that simply aren't present. and the top answer provides this tidyverse solution:Would it be possible to implement this as one function like this?I have to use this a lot and it would be really handy if it were all combined into one.You can do this by using quosures. Here’s a motivating example, using some made-up data. This is great. Related. It lets us do this:#> The following objects are masked from 'package:dplyr':#> The following object is masked from 'package:purrr':#> sex race stratum income_mean_BA income_mean_HS income_length_BA#> 1: F B 1 101.56368 115.51767 16#> 2: F B 2 92.97993 90.54429 12#> 3: F B 3 114.20984 103.88870 18#> 4: F B 4 90.51281 103.28220 13#> 5: F B 5 110.94754 70.65812 12#> 6: F B 6 97.82804 88.66467 9 A tidyverse-only way to spread() with multiple value columns. Note that there are two types of missingness in the input: explicit missing values (i.e. Description Usage Arguments Examples.

0 votes . both A and B, such that the output is something like. Of these, everything is categorical except income. But how can I spread two values e.g. 1 view. In particular, he needed to spread the average income and count values by education into columns. I coincidentally just watched Hadley Wickham's video on Tidy Evaluation this morning so this makes a lot more sense than it would have a week ago. Both types of missing value will be replaced by fill. And there we are. R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R … Spread a key-value pair across multiple columns Source: R/spread.R.

convert. I think that the reprex ( We have a tool for this sort of thing (with a diagrammatic theory) This is an ooooooold thread, but I'm facing the same problem and decided to If you are stumbling on this thread, you may want to read through the pivot functions vignette: Here are some very simple worked examples using the new pivot functions: One example involves multiple columns, as framed in the original question here: Development on spread() is complete, and for new code we recommend switching to pivot_wider(), which is easier to use, more featureful, and still under active development. In tidyr: Tidy Messy Data. Share Tweet. Dan Sullivan has The multi-spread function generalizes to more than two values, by the way. Here’s what it looks like:In order to do a bit of modeling, what Steve wanted to do was to get a table with the data grouped by sex, race, and stratum, but with averages and totals for income by education.

View source: R/spread.R.

If set, missing values will be replaced with this value. What we have are measures of sex, race, stratum (from a survey, say), education, and income. To leave a comment for the author, please follow the link and comment on their blog: R on kieranhealy.org. month Amy.A Bob.A Amy.B Bob.B. In effect—and this is a general strategy when doing this kind of thing with First we gather the summaries (mean income and N observations) for each value of the education variable, still stratifying on sex, race, and stratum:Because we might be doing this gather-unite-spread step quite often, it’d be useful to have a function to bundle up the steps for us into something more convenient. spread.Rd.