0

I have a 2 column dataframe that I want to become 2 rows instead. The dimensions are 40 observations and 2 variables (whereas what I want is 40 variables and 1 observation, as I want the first row to become the column headers).

I have tried using reshape but I can't seem to get it right as I keep getting errors. Here is the dataframe that I am using:

      Data.Label                      Test2
1     Family                          Petromyzontidae - lampreys
2     Species                         Ichthyomyzon castaneus
3     Taxonomic Authority             Girard, 1858
4     Common Name(s)                  Chestnut Lamprey
5     French Name                     lamproie brune
6     OMNR Code                       016
Test5 <- reshape(Test4, timevar = Test4$Data.Label, idvar = Test4$Test2,direction = "wide")
Error in `[.data.frame`(data, , idvar) : undefined columns selected

As mentioned, I've tried to use reshape. As there are 40 variables that I would like to become the column headers (Test4$Data.Label) I did not want to write out each one for the idvars. Is there another way to do this?

The desired result would be:

Family                     Species                Taxonomic Authority                Common Name(s)                French Name                OMNR Code                      
Petromyzontidae lampreys   Ichthyomyzon castaneus  Girard, 1858                      Chestnut Lamprey             lamproie brune              016

This way the Test4$Data.Label column becomes the column headers (40 in total) and the Test4$Test2 column becomes the 1st row.

temsandroses
  • 311
  • 1
  • 3
  • 11

1 Answers1

1

An option would be to deframe and convert to list

library(tidyverse)
deframe(df1) %>%
      as.list %>% 
      as_tibble
# A tibble: 1 x 6
#  Family                   Species              `Taxonomic Authorit… `Common Name(s)` `French Name` `OMNR Code`
#  <chr>                    <chr>                <chr>                <chr>            <chr>         <chr>      
#1 Petromyzontidae - lampr… Ichthyomyzon castan… Girard, 1858         Chestnut Lamprey lamproie bru… 016      

data

df1 <- structure(list(Data.Label = c("Family", "Species",
"Taxonomic Authority", 
"Common Name(s)", "French Name", "OMNR Code"), 
 Test2 = c("Petromyzontidae - lampreys", 
"Ichthyomyzon castaneus", "Girard, 1858", "Chestnut Lamprey", 
"lamproie brune", "016")), class = "data.frame", row.names = c("1", 
"2", "3", "4", "5", "6"))
akrun
  • 874,273
  • 37
  • 540
  • 662