Showing posts with label R programming language. Show all posts
Showing posts with label R programming language. Show all posts

Sunday, August 2, 2015

R Programming - Reading from a csv file

Refer index page for R at the following URL:
http://mylearningcafe.blogspot.in/2015/07/r-programming-index-page.html 
 



Basic requirement would be to read data from a CSV file into a data frame.

Command to read from a csv file?
csv_file <-read.csv("file_name.csv",TRUE,",")

What are the input parameters?
The input parameters to the read.csv functionis
  • The file name from the working directory (or a full URL)
  • If the first line of the csv contains the header, use TRUE else FALSE
  • The delimiter. In case of csv its a comma (,)
How to find the working directory?
Use the command getwd()

How to change the working directory?
Use the setwd(dir) for the same
e.g setwd("C:/TestFolder")

 

I have saved the following csv in the working directory





To read the same, I will use the following command
csv_file <-read.csv("test.csv",TRUE,",")



Note that I have used the head and tail command to retrieve a snapshot (6 rows)

One can also use a URL to read a file

csv_file <-read.csv("file_url\file_name.csv",TRUE,",")


Refer index page for R at the following URL:
http://mylearningcafe.blogspot.in/2015/07/r-programming-index-page.html 
 

Thursday, July 30, 2015

R programming - Lists

Refer index page for R at the following URL:
http://mylearningcafe.blogspot.in/2015/07/r-programming-index-page.html 


What are lists?
Lists are similar to vectors (list of data) but they have an advantage over vector in terms of the data type.

Vectors store data types of same form (e.g you could have a vector of numbers or vector of characters).
In a List, you could have various data types and also store another list or a data frame within it.

How to create a list?
mylist<-list(element1,element2,element3...)

We had created a data frame x in the last chapter (http://mylearningcafe.blogspot.in/2015/07/r-programming-data-frames.html).

We shall add that data frame to a list




In the above image, you can see that when we print a list, we can see the number of elements being displayed. In the above one, we created a list containing a number, a character, a vector and a data frame.

If you want to access the data of a specific element, use listname[[element_id]].
You need to use double square brackets [[ ]]



How to find the number of elements in a list?
Use the length(list_name) function

Refer index page for R at the following URL:
http://mylearningcafe.blogspot.in/2015/07/r-programming-index-page.html