R Data Importing
Data Importing
- R can read data from external files, stored outside the R environment
- R has the capability to work with data in any format, such as csv, xlsx, json etc…
- Steps to read a file are as follows:
- Step 1: Set the path of the location to the file’s location
- getwd() # gives the current directory
- setwd(“path”) # sets the path, e.g.setwd(“D:/R/dataset/”)
- Step 2: Ensure the relevant package is installed
- library () # shows the packages that are loaded
- for xlsx and json additional packages must be installed
- Step 3: Use ‘read’ command to assign data to an object
- read.csv () # reads the .csv file
- read.xlsx () # reads the .xlsx file
- read.table() # reads the .txt file
If we need to upload the file ‘Truck’ from “d:/R/” location
Step 1: Set the path
Step 2: Ensure xlsx package is installed
Step 3: Use read.xlsx() from xlsx package
Syntax
tk <- read.xlsx(file = "truck.xlsx", sheetName = "Sheet1")