Here's an example using pivot longer:
Do you want to pivot the column NationalDiagnosis
then use the 2nd pivot function transforming the values
into characters (instead of numeric).
library(tidyverse)
df <- read_csv("https://raw.githubusercontent.com/Chazzer90/stackoverflowhelp2/main/SEQ_anom.csv")
#> Parsed with column specification:
#> cols(
#> `<ef>..MergeRecno` = col_double(),
#> MergeEncounterRecno = col_double(),
#> SequenceNo = col_double(),
#> DiagnosticSchemeCode = col_double(),
#> DiagnosisCode = col_double(),
#> DiagnosisSiteCode = col_character(),
#> NationalDiagnosisCode = col_double(),
#> NationalDiagnosis = col_character()
#> )
df %>%
mutate(DiagnosisSiteCode = as.integer(ifelse(DiagnosisSiteCode == "NULL", NA, DiagnosisSiteCode))) %>%
pivot_longer(cols = DiagnosticSchemeCode:NationalDiagnosisCode,
names_to = 'variables', values_to = 'Values',
values_drop_na = TRUE,
names_ptypes = list(Values = integer()))
#> # A tibble: 134 x 6
#> `\xef..MergeRec~ MergeEncounterR~ SequenceNo NationalDiagnos~ variables
#> <dbl> <dbl> <dbl> <chr> <chr>
#> 1 402 545353 1 Muscle/tendon i~ Diagnost~
#> 2 402 545353 1 Muscle/tendon i~ Diagnosi~
#> 3 402 545353 1 Muscle/tendon i~ Diagnosi~
#> 4 402 545353 1 Muscle/tendon i~ National~
#> 5 758 261891 1 Cardiac conditi~ Diagnost~
#> 6 758 261891 1 Cardiac conditi~ Diagnosi~
#> 7 758 261891 1 Cardiac conditi~ National~
#> 8 894 941852 1 Respiratory con~ Diagnost~
#> 9 894 941852 1 Respiratory con~ Diagnosi~
#> 10 894 941852 1 Respiratory con~ Diagnosi~
#> # ... with 124 more rows, and 1 more variable: Values <dbl>
## do you want to pivot the column NationalDiagnosis
df %>%
mutate(DiagnosisSiteCode = as.integer(ifelse(DiagnosisSiteCode == "NULL", NA, DiagnosisSiteCode))) %>%
pivot_longer(cols = DiagnosticSchemeCode:NationalDiagnosis,
names_to = 'variables', values_to = 'Values',
values_drop_na = TRUE,
values_transform = list(Values = as.character))
Created on 2020-10-21 by the reprex package (v0.3.0)