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 do I separate data from one column into two

I have a column data$Floor from a dataset imported from CSV that supposedly contains the floor of an apartment. Here is a sample data:

Floor
Ground out of 2
1 out of 3

I wish to separate the data to have the floor and the total floors as following:

Floor Total Floors
Ground 2
1 3

I have done write(str_replace_all(data$Floor, "out of", " "), data) with the intention of splitting the columns where the space is, then writing the changes to the dataset data but I have no idea how to do that.

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 :

You can use tidyr::separate:

tidyr::separate(data, Floor, c('Floor', 'Total Floors'), sep = ' out of ')
#>    Floor Total Floors
#> 1 Ground            2
#> 2      1            3

Data used

data <- data.frame(Floor = c("Ground out of 2", "1 out of 3"))

data
#>             Floor
#> 1 Ground out of 2
#> 2      1 out of 3
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