Reading Text Files in R

It is pretty straight forward to read csv files in R – simply give the source of the data file that be in

trial1 = read.csv(“data\\sample1.csv”)

With importing text files in R, you got same default parameters to play around like if the file includes the header. The strip.white allows you to indicate whether you want the white spaces from unquoted character fields stripped. In the following example, NA value is set as “EMPTY” with na.strings parameter.

trail2 = read.table(“data\\sample2.txt”, header=TRUE, sep=”/”, strip.white=TRUE, na.strings=”EMPTY”)

One other variation is read.csv2. This function is used when the files contains data in a different locale that has comma as decimal points and semicolon as separator.

trial3 = read.csv2(“data\sample3.csv”, header=TRUE, strip.white=TRUE)

Quick tips to remember when prepping your datasheet are:

  • Use short name for the files
  • Do not use blank spaces in names, values or fields
  • Avoid using names with special characters
  • Indicate missing values with NA
  • Delete comments in excel files to avoid extra columns or NAs
Advertisements
This entry was posted in R and tagged . Bookmark the permalink.

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 )

w

Connecting to %s