can anyone help me tidy this dataset in R?
This is the original 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 trying to convert it to this 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
Thanks!
I've been trying to use pivot_longer but keep receiving errors from R
library(tibble)
library(dplyr)
library(tidyr)
mydata <- tribble(
~famid, ~named, ~ incd, ~namem, ~incm,
1, "Bill", 30000, "Bess", 15000,
2, "Art", 22000, "Amy", 18000,
3, "Paul", 25000, "Pat", 50000)
mydata |>
rename(name_d = named, inc_d = incd, name_m = namem, inc_m = incm) |>
pivot_longer(!famid, names_to = c(".value", "parent"), names_sep = "_")
# A tibble: 6 × 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