how to import data in r

To import data in R, one common method is to use the read.csv() function to read in a comma-separated values (CSV) file as a data frame object. Here is an example code:

main.r
# set the working directory to where the file is located
# setwd("path/to/working/directory")

# read in the CSV file as a data frame
my_data <- read.csv("my_data.csv")

# view the first few rows of the data frame
head(my_data)
228 chars
9 lines

This code assumes that the file my_data.csv is located in the working directory. You can also specify the full file path if the file is located elsewhere.

There are other functions that can be used to import data in R depending on the file format, such as read.table() for tab-delimited files and read.xlsx() for Excel files. Check out the readr and rio packages as well for other data import options.

gistlibby LogSnag