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

Stack top two rows (including column name) onto other dataframe

I have two data frames:

df<-structure(list(`Active Contact*` = "Entries must be in a Yes or No format. Only active staff may be added to a protocol.", 
    `First Name*` = "Free text field. [255]", `Middle Name

` = "Free text field. [255]", 
    `Last Name*` = "Free text field. [255]", `Email**

` = "This field is required when the contact is a user or the contact has any of the Receives Broadcast Emails, Receives Notifications, or Receives Administrative System Notifications settings set to Yes.\r\nThis field must be mapped if Email is selected in the Unique Identifier field. Entries must be unique across all contacts (both active and inactive) and must be in a valid email format (abc@efg.zyx).  [254]"), row.names = c(NA, 
-1L), class = c("tbl_df", "tbl", "data.frame"))

df2<-structure(list(ActiveContact = c("Yes", "Yes", "Yes", "Yes", 
"Yes", "Yes", "Yes"), fname = c("practice", "practice", "practice", 
"practice", "practice", "practice", "practice"), middlename = c(NA, 
NA, NA, NA, NA, NA, NA), lname = c("PI", "research nurse", "research nurse", 
"research nurse", "regulatory", "regulatory", "regulatory"), 
    email = c("ppi@lifespan.org", "prn@lifespan.org", "prn@lifespan.org", 
    "prn@lifespan.org", "preg@lifespan.org", "preg@lifespan.org", 
    "preg@lifespan.org")), row.names = c(NA, -7L), class = c("tbl_df", 
"tbl", "data.frame"))

I need to use the the column name from df, and also the first row from df…. as column name and first row in df2 (replacing the column name from df2, and also pushing the first row in df2 down 1 row to fit).

My expected output would be:
enter image description here

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

I know the column names are terrible (weird symbols and spaces and things I hate), and also I know the first row that I need is full of all sorts of stuff I typically hate, but I need this for my output format.

Thank you!

>Solution :

You can try to row bind them, simultaneously renaming the columns of df2

rbind(df,setNames(df2,names(df)))

Output:

  `Active Contact*`                  `First Name*`    `Middle Name\n           ~ `Last Name*`   `Email**\n                     \n               ~
  <chr>                              <chr>            <chr>                      <chr>          <chr>                                            
1 Entries must be in a Yes or No fo~ Free text field~ Free text field. [255]     Free text fie~ "This field is required when the contact is a us~
2 Yes                                practice         NA                         PI             "ppi@lifespan.org"                               
3 Yes                                practice         NA                         research nurse "prn@lifespan.org"                               
4 Yes                                practice         NA                         research nurse "prn@lifespan.org"                               
5 Yes                                practice         NA                         research nurse "prn@lifespan.org"                               
6 Yes                                practice         NA                         regulatory     "preg@lifespan.org"                              
7 Yes                                practice         NA                         regulatory     "preg@lifespan.org"                              
8 Yes                                practice         NA                         regulatory     "preg@lifespan.org"                             
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