Showing posts with label R. Show all posts
Showing posts with label R. 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 
 

Tuesday, July 7, 2015

R programming - Vectors

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

Vectors are important data types in R.
Vectors can be thought of as a list of items of any data type. However, it cannot contain a mix of data types. Thus, you could have a vector of numerics, string etc but not a mix.

How to define or create a vector?
Vectors are created using the c (...) command

Thus to create a vector of 3 items (1,2,3) we need to run the following command

x<-c(1,2,3)
x<-c("Hi","how","are","you","?")




One cool feature is that we can create a vector of large numbers by providing a range

x<-c(1:5) or x<-1:5
This will create a vector of 5 elements

We can also create a list of negative values if needed



Another cool option that R provides in Vectors is that we can do mathematical operations on all the elements in a vector.

x<-1:3
x*2




One can also add/subtract same length vectors



What happens if the length differs?
In such a case, the function will be applied till the length matches, then an error is thrown.

Lets have 2 vectors
x<1:3  (length =3)
z<-1:5 (length =5)
x+z will add the first 3 elements and then throw an error




How to check length of a list?
Use the length command

length(x)



How to get specific elements from a list?
Use the command list_name[char number]

x[2]
z[1:3] will return first 3 elements



Check for elements within a Vector?

Checks are performed for each element of a vector.

x<-1:5
x<=2 will return TRUE TRUE FALSE FALSE FALSE

We also have any and all commands

any(x<3) will look if any element is less than 3 and return TRUE/FALSE. In this case, TRUE is returned.

all(x<3) will look if all elements are less than 3 and return TRUE/FALSE. In this case, FALSE is returned.



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

R programming - more about variables

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

In the previous article, we saw how to assign and remove variables.
We shall continue a bit more on variables.

Since there are multiple data types, one can see the data types using certain functions
  • class(variable_name) gives the data type of the variable
  • is.numeric(variable_name) returns TRUE/FALSE based on the nature of the variable
  • nchar(variable_name) gives the count of the number of characters in the variable
  • ls() will list all the variables in your workspace
    • ls() returns character(0) if no variables are found
  • ls.str() will list more details of the variables in your workspace
  •  rm(list=ls()) can be used to remove all the variables in your workspace










Dates
Dates can be defined as

x<-as.Date("2015-07-20") [Note that D is capital]



Vectors are important data types in R.
We shall discuss more about vectors in upcoming articles.

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