Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Pivot Dataframe with different column names

I have a dataframe similar to this:

> dput(df)
structure(list(Name = "Jon", Age = 23, Name1 = "Jessica", Age1 = 13, 
    Name2 = "Martin", Age2 = 54), class = "data.frame", row.names = c(NA, 
-1L))

I want to pivot it longer, so that all of the names are in one column, and all of the ages are in another column. Im finding this hard to do since the column names differ.

Desired output:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

     Name Age
1     Jon  23
2 Jessica  13
3  Martin  54

Thank you!

>Solution :

library(tidyr)
pivot_longer(df, everything(),
             names_pattern = "([^0-9]+)[0-9]*$",
             names_to = ".value")
# # A tibble: 3 x 2
#   Name      Age
#   <chr>   <dbl>
# 1 Jon        23
# 2 Jessica    13
# 3 Martin     54

or with data.table::melt:

data.table::melt(
  as.data.table(df),
  measure.vars = patterns(Name="^Name", Age="^Age"))
#    variable    Name   Age
#      <fctr>  <char> <num>
# 1:        1     Jon    23
# 2:        2 Jessica    13
# 3:        3  Martin    54

(and we can get rid of the variable column fairly easily)

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading