Splitting one Excel data frame to multiple data frames

I have one excel sheet that contains two tables separated by two empty rows (NAs) that is similar to this:

x <- c("Speed", "Fuel_level", "OpHours", NA, NA, "Warning", "TempSensor", "TripDistance")
y <- c("kmph", "liter", "hr", NA, NA, "msg", "C", "km")
df <- data.frame(x,y)

Is there a function in R where I can split this data frame on NAs into two data frames to look like this?

             x     y
1        Speed  kmph
2   Fuel_level liter
3      OpHours    hr
             x     y
1     Warning    msg
2   TempSensor     C
3 TripDistance    km

A simple way, but you have to know which rows have the NAs.


library(dplyr)
x <- c("Speed", "Fuel_level", "OpHours", NA, NA, "Warning", "TempSensor", "TripDistance")
y <- c("kmph", "liter", "hr", NA, NA, "msg", "C", "km")
df <- data.frame(x,y)
slice(df, 1:3)
#>            x     y
#> 1      Speed  kmph
#> 2 Fuel_level liter
#> 3    OpHours    hr
slice(df, 6:8)
#>              x   y
#> 1      Warning msg
#> 2   TempSensor   C
#> 3 TripDistance  km

Created on 2022-06-21 by the reprex package (v2.0.1)

2 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.