Home > front end >  Stack top two rows (including column name) onto other dataframe
Stack top two rows (including column name) onto other dataframe

Time:03-29

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 ([email protected]).  [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("[email protected]", "[email protected]", "[email protected]", 
    "[email protected]", "[email protected]", "[email protected]", 
    "[email protected]")), 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

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!

CodePudding user response:

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             "[email protected]"                               
3 Yes                                practice         NA                         research nurse "[email protected]"                               
4 Yes                                practice         NA                         research nurse "[email protected]"                               
5 Yes                                practice         NA                         research nurse "[email protected]"                               
6 Yes                                practice         NA                         regulatory     "[email protected]"                              
7 Yes                                practice         NA                         regulatory     "[email protected]"                              
8 Yes                                practice         NA                         regulatory     "[email protected]"                             

CodePudding user response:

names(df2) <- names(df)
df3 <- rbind(df, df2)
  •  Tags:  
  • r
  • Related