Home > OS >  Import two dataframes from the same excel sheet in Rstudio as separated dataframes
Import two dataframes from the same excel sheet in Rstudio as separated dataframes

Time:11-18

I have an excel file named "inner_merge" which in first sheet named "original" contains 2 separated dataframes exactly like in the image below. The first one has columns id,type,state,zip and the second id1,type1,city1,state1,zip1,data,data_1,data_2 I would like to know how could I import those two in Rstudio as separated dataframes, avoiding to create two separated excel files.

enter image description here

CodePudding user response:

With readr::read_excel you can use the range argument to specify which cells to read in. So you can use it once specifying columns A-E and again specifying columns H-P. See ?read_excel and ?cell-specification for details.

From your picture, you want something like this:

library(readxl)
library(cellranger)

df1 = read_excel(path, range = cell_cols("A:E"))
df2 = read_excel(path, range = cell_cols("H:P"))

Where path is the path to your excel file. You may also need to specify which sheet to read from if there are more than one.

  • Related