Tidy dadmom dataset?

. Can anyone help me convert the following dataset to a more tidy form?

## # A tibble: 3 x 5
    ##   famid named  incd namem  incm
    ##   <dbl> <chr> <dbl> <chr> <dbl>
    ## 1     1 Bill  30000 Bess  15000
    ## 2     2 Art   22000 Amy   18000
    ## 3     3 Paul  25000 Pat   50000

I’m aiming to convert it to the following form:

## # A tibble: 6 x 4
    ##   famid parent name    inc
    ##   <dbl> <chr>  <chr> <dbl>
    ## 1     1 d      Bill  30000
    ## 2     1 m      Bess  15000
    ## 3     2 d      Art   22000
    ## 4     2 m      Amy   18000
    ## 5     3 d      Paul  25000
    ## 6     3 m      Pat   50000

I’ve been trying to use pivot_longer from the tidyr package in R to achieve this, but keep receiving errors. Can anyone help me get this dataset into a more tidy form?

To convert the dataset to a more tidy form, you can use the pivot_longer function from the tidyr package in R. Here’s the code to achieve the desired result:

library(tidyr)

# Original dataset
original <- tribble(
  ~famid, ~named, ~incd, ~namem, ~incm,
  1, "Bill", 30000, "Bess", 15000,
  2, "Art", 22000, "Amy", 18000,
  3, "Paul", 25000, "Pat", 50000
)

# Convert to tidy form
tidy <- original %>%
  pivot_longer(cols = -famid, 
               names_to = c(".value", "parent"), 
               names_pattern = "(.*)\\.(.*)")

# Print the tidy dataset
tidy

This will give you the following tidy dataset:

# A tibble: 6 x 4
  famid parent named  inc
  <dbl> <chr>  <chr> <dbl>
1     1 d      Bill  30000
2     1 m      Bess  15000
3     2 d      Art   22000
4     2 m      Amy   18000
5     3 d      Paul  25000
6     3 m      Pat   50000

Hope this helps!