Press "Enter" to skip to content

How to Read data in Rstudio?

Zigya Acadmey 0

R is capable of reading data from most formats, including files created in other statistical packages. Whether the data was prepared using Excel (in CSV, XLSX, or TXT format), R can read and load the data into memory.

R also has two native data formats Rdata (sometimes shortened to Rda) and Rds. 

Before reading any data in Rstudio you first need to check that you are in the same directory that as of the Data you want to load.

> getwd()
[1] "C:/Users/Admin/Desktop"
> setwd("C:/Users/Admin/Desktop/zogya_d")  # the data is present in the folder name zigya_d

Reading Local .txt file

Function: read.table()

Common Parameters:

  • Header: TRUE when the first row includes variable names. The default is FALSE.
  • Sep: A string indicating what is separating the data. The default is ”  “.

To import a word.txt file from your local, the syntax would be:

# Read a txt file, named "mtcars.txt"
 and storing it in vector named my_data
> my_data <- read.tabe("word.txt", header=TRUE, sep= " ")

By default numeric items (except row labels) are read as numeric variables. We can change this if we want.

Reading Local .csv file

Function: read.csv()

Common Parameters:

  • Header: TRUE when first row includes variable names. The default is FALSE.
# Read a csv file, named "word.csv"
 and storing it in vector named my_data
> my_data <- read.csv("word.csv")

Conclusion

We covered how to read data from .txt files and .csv files as well, along with the change the directory in which the data is present.

This brings the end of this Blog. We really appreciate your time.

Hope you liked it.

Do visit our page www.zigya.com/blog for more informative blogs on Data Science

Keep Reading! Cheers!

Zigya Academy
BEING RELEVANT

Leave a Reply

Your email address will not be published.