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

how to bind rows into one row but still keep original rows

I have a df that looks like the on the left. I would like to put all rows inputs into one row,and still keep old rows. How should I do this?

enter image description here

df<-structure(list(c1 = c("Student Enrollment", "750"), c2 = c("Faculty Number", 
"21"), c3 = c("Graduated", "65")), row.names = c(NA, -2L), class = c("tbl_df", 
"tbl", "data.frame"))

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

>Solution :

output = rbind(df, lapply(df, paste, collapse = "\n"))
output
#                        c1                 c2            c3
# 1      Student Enrollment     Faculty Number     Graduated
# 2                     750                 21            65
# 3 Student Enrollment\n750 Faculty Number\n21 Graduated\n65

In the console, line breaks print as \n. If you View() it they will print as spaces. There are probably some nice table formatting packages that actually display linebreaks within a cell.

You could, of course, replace "\n" in my code with " " to use a space instead of a linebreak.

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