Importing Data into R/ R-studio

Importing data is the basic step before starting the analysis, but it can be frustrating sometimes.

So, lets see, how we can import our data into R- environment.

1. In R –studio, you can go to environment on the right side of the panel and click on the import dataset and then you can choose one out of the three options like: text to import the data (csv files), or you can choose excel to import the excel files or other statistical files (SAS, SPSS, Stata etc).

Screen_Shot_2018-10-31_at_9.24.22_PM.png

2. You can also go the file section of the R- studio on the top menu bar and select the import function to import the data.

Screen_Shot_2018-10-31_at_9.28.55_PM.png

3. You can also import your .txt file using the read.table() function.

df <- read.table(“file name”, header = FALSE)

as a default, header is always set at TRUE. header will show you the header with variables name.

4. You can read .csv file with read.table () or read.csv() or read.csv2()

Note: You could land yourself in trouble if you have saved your file in Byte Order Mark (BOM). if you have done this, then you have to add an extra argument “ fileEncoding = “UTF-8-BOM” to your function

for read.table() = you have to specify the separator character (for csv generally separators are “,” or “’;”

example,

df <- read.table(“file name”,
                  header = FALSE,
                  sep = “,”)

read.csv() = for file with “,” as separator

read.csv2() = for files with “;” as separator

df <- read.csv(“file name”,
                header = FALSE)

df <- read.csv2(“file name”,
                header= FALSE)

“ Note that if you get a warning message that reads like “incomplete final line found by readTableHeader”, you can try to go and “stand” on the cell that contains the last value (c in this case) and press ENTER. This will normally fix the warning because the message indicates that the last line of the file doesn’t end with an End Of Line (EOL) character, which can be a linefeed or a carriage return and linefeed. Don’t forget to save the file to make sure that your changes are saved!

Pro-Tip: use a text editor like NotePad to make sure that you add an EOL character without adding new rows or columns to your data.”

5. If your data file is separated with characters other than tab, comma and semicolon, you can use read.delim() or read.delim2()

df <- read.delim(“file name, sep=”$”)

df <- read.delim2(“file name”, sep=”$”)

You can get more info on other types of file: like xml, json from https://www.datacamp.com/community/tutorials/r-data-import-tutorial

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s