Now, I would like to usepivot_wider to keep the name and age_group but "compute" a new variable with the t2 result of the parents_total. In this example, age_group may change. If possible, I would like to compute this "t2" variable if age_group remained the same.
where each row is one country-year and the columns represent the different variables with a suffix for each group. I think I'll have to use pivot_wider() but couldn't figure out how to preserve the country-year combination. Can someone point me into the right direction?
Is there a way to use pivot_wider() to widen ALL columns except for any columns that I specify as columns that uniquely identify each observation using id_cols (i.e., ID and time). I’d like the solution to be extendable to the case where I have many columns (and thus do not want to specify variable names or ranges for variables to be widened). Oh, I see, then there is another problem with names_pattern.I'm unsure what you expect it to do, but it actually expect one string defining a name regexp patttern for breaking some columns into multiple "names" columns (see e.g. the two last examples in the man page of pivot_longer).
First, transform the dataframe into one with only three columns id, nameyear, and value; concurrently separate the second column nameyear into name and year. Then, just pivot the two columns name and value wider. Output. # A tibble: 14 x 4 id year emp marstat 1 1 1 ft married 2 1 2 ft divorced 3 2 1 ft married 4 2 2 ft

The function works like this: pivot_wider(data = data.frame, id_cols = identifying_columns, names_from = "Col with Names", values_from = "Col with Values") data. is just the data frame you want to reshape. id_cols. lists the columns that contain essential identifying information for each observation. names_from.

. 330 126 38 739 596 602 850 413

how to use pivot_longer in r