Home > Net >  Pivot Dataframe with different column names
Pivot Dataframe with different column names

Time:12-08

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:

     Name Age
1     Jon  23
2 Jessica  13
3  Martin  54

Thank you!

CodePudding user response:

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)

  • Related