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 make column value a new row in r

I have a dataframe that I would like to reformat by making the first column its own row above the values.

I want this:

| Type | Value1 | Value2 |
| -----| ------ | ------ |
| A    | 1      | 3      |
| B    | 2      | 2      |

To become this with the rows containing "A" and "B" to be merged cells:

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

|      | Value1 | Value2 |
| -----| ------ | ------ |
| A                      |
|      | 1      | 3      |
| B                      |
|      | 2      | 2      |

>Solution :

We could use insertRows function from berryFunctions package:
For your original data you may adapt c(1,3) -> for example with a sequence:

library (berryFunctions)
librar(dplyr)

insertRows(df, c(1,3), new="") %>% 
  mutate(Type = lead(Type, default = ""))
  Type Value1 Value2
1    A              
2           1      3
3    B              
4           2      2
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