Jamie
Jamie

Reputation: 553

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:

     Name Age
1     Jon  23
2 Jessica  13
3  Martin  54

Thank you!

Upvotes: 0

Views: 81

Answers (1)

r2evans
r2evans

Reputation: 160447

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)

Upvotes: 4

Related Questions