0

I'm trying to get my data frame to go from a wide format to long format, but without consistent column name structures nor consistent data types (as I've seen in other Github examples)

I tried transposing the table, but the format does not keep the 'Number' or the Column names within the table.

Number   First_Name   Birth_Date    Last_Name    Blood_Type    Race         Sex
1        Joe          1995-01-01    Smith        O-            Asian        Male
2        Bob          1992-01-01    Bobson       A             Caucasian    Male
3        Jane         1990-01-01    Johnson      B+            Black        Female
1 First_Name Joe
1 Last_Name Smith
1 Birth_Date 1995-01-01
1 Blood_Type O-
1 Race Asian
1 Sex Male

2 First_Name Bob
2 Last_Name Bobson
2 Birth_Date 1992-01-01
2 Blood_Type A
2 Race Caucasian
2 Sex Male

3 First_Name Jane
3 Last_Name Johnson
3 Birth_Date 1990-01-01
3 Blood_Type B+
3 Race Black
4 Sex Female
Theresa
  • 65
  • 5

1 Answers1

2

We can use pivot_longer excluding the first column

library(tidyr)
pivot_longer(df1, cols = -Number)

-output

# A tibble: 18 x 3
   Number name       value     
    <int> <chr>      <chr>     
 1      1 First_Name Joe       
 2      1 Birth_Date 1995-01-01
 3      1 Last_Name  Smith     
 4      1 Blood_Type O-        
 5      1 Race       Asian     
 6      1 Sex        Male      
 7      2 First_Name Bob       
 8      2 Birth_Date 1992-01-01
 9      2 Last_Name  Bobson    
10      2 Blood_Type A         
11      2 Race       Caucasian 
12      2 Sex        Male      
13      3 First_Name Jane      
14      3 Birth_Date 1990-01-01
15      3 Last_Name  Johnson   
16      3 Blood_Type B+        
17      3 Race       Black     
18      3 Sex        Female    

data

df1 <- structure(list(Number = 1:3, First_Name = c("Joe", "Bob", "Jane"
), Birth_Date = c("1995-01-01", "1992-01-01", "1990-01-01"), 
    Last_Name = c("Smith", "Bobson", "Johnson"), Blood_Type = c("O-", 
    "A", "B+"), Race = c("Asian", "Caucasian", "Black"), Sex = c("Male", 
    "Male", "Female")), class = "data.frame", row.names = c(NA, 
-3L))
akrun
  • 874,273
  • 37
  • 540
  • 662
  • 1
    This is perfect, thank you! Just a note that I had to change all data types (ie. dates) to characters for this to work. – Theresa Jul 05 '21 at 20:23
  • @Theresa Yes, here we assumed that all columns are characters. If some are not, pivot_longer will return error – akrun Jul 05 '21 at 20:24