load an excel file in r

To load an Excel file in R, we can use the readxl package. We first need to install and load the package using the following code:

main.r
install.packages("readxl")
library(readxl)
43 chars
3 lines

To read an Excel file, we use the read_excel() function. Here's an example of how to read an Excel file named "data.xlsx" from the working directory:

main.r
data <- read_excel("data.xlsx")
32 chars
2 lines

By default, read_excel() reads the first sheet in the Excel file. If you want to read a specific sheet, you need to specify the sheet name or index:

main.r
data <- read_excel("data.xlsx", sheet = "Sheet1")
50 chars
2 lines
main.r
data <- read_excel("data.xlsx", sheet = 2)
43 chars
2 lines

You can also specify the range of cells to read:

main.r
data <- read_excel("data.xlsx", range = "A1:C10")
50 chars
2 lines

Finally, you can specify the column types using the col_types parameter:

main.r
data <- read_excel("data.xlsx", col_types = c("numeric", "text", "date"))
74 chars
2 lines

These are just some examples of the options available in read_excel(). For more information, you can check the help documentation using ?read_excel.

gistlibby LogSnag