You are on page 1of 5

What,Why and How

Well! R is a programming Language (functional and Object Oriented features


availaible ) that also provides excellent Data Visualization tool. With increasing
demand for Data Driven Decision makers, R is becoming increasingly popular.
Letting the Whys and Whats to rest, we concentrate on how to use the
features availaible or build our own methods and functions, if need arises.

Objects and Expression


We start with the assignment statements. Run the following chunk in the console.
> product <- "chair"
> quantity <- 6
> price <- 645
Now we list the objects in the memory and check the structure of the objects
that are created. Note thet numeric data types can be further classified.
> ls()
[1] "price"

"product"

"quantity"

> str(product)
chr "chair"
> str(quantity)
num 6
> str(price)
num 645
> typeof(quantity)
[1] "double"
Length() method is called on vector objects and returns the length of the vector.
nchar() method returns the number of characters in the string. Note that R does
not treat string as a vector of character.
> length(product)
[1] 1
> nchar(product)
1

[1] 5
An example of the logical object.
> availaible <- TRUE
> typeof(availaible)
[1] "logical"
To define a vector in R we need to combine (R command : c()) various
objects of the same data type. Once we have a vector we will treat it as a data
and try to calculate some statistic and play with some of the subaddressing
mechanisms.
> prices <- c(645,600,750,231)
> mean(prices)
[1] 556.5
> prices[1]
[1] 645
> prices[2:3]
[1] 600 750
> prices[-2]
[1] 645 750 231
> prices[length(prices)]
[1] 231
> print(prices[prices>400])
[1] 645 600 750
Creating a character vector (represents the types of products sold). Creating A
vector of order quantities and then using R as a calculator to evaluate the total
price.
> types <-c("chair", "tool", "table", "accessory")
> amounts <- as.integer(c(4, 2, 1,6))
> values <- prices * amounts
Element by element multiplication
> print (values)
2

[1] 2580 1200

750 1386

> print(sum(values))
[1] 5916
Alternative to this is matrix multiplication
>
>
>
>

matrixprices<- matrix(prices,nrow=1)
matrixamounts <- matrix(amounts,ncol=1)
values <- matrixprices %*% matrixamounts
print(values)

[,1]
[1,] 5916
Now we go see more of matrix. We plan to create a matrix with each row as
the data of amounts from different stores.
> amounts<- c(amounts,c(3,2,5,7))
> store.sales <- matrix(amounts, nrow=2, ncol=4)
> print(store.sales)

[1,]
[2,]

[,1] [,2] [,3] [,4]


4
1
3
5
2
6
2
7

> values <- store.sales %*% prices


>
Now, we name the column and rows of the matrix. Lets also determine how
many total chair were sold. We will use matrix slicing notation and the sum()
function to accomplish this task. Alternatively, we can use colsum method
> dimnames(store.sales) <-list(c("Billekhalli Store", "Arakeri Store"), types)
> store.sales[2,2]
[1] 6
> store.sales["Arakeri Store","table"]
[1] 2
> sum(store.sales[,1])
[1] 6
> sum(store.sales[,"chair"])
[1] 6
3

>
We will now see how the list works. For this, we consider the e-retailers for
thinks in our stores.
>
>
>
>

eretailer <- c("flipkart","house-furniture","snapdeal")


penetration <- c(40,20,30)
dataanalyst <- list(eretailer,penetration)
dataanalyst

[[1]]
[1] "flipkart"

"house-furniture" "snapdeal"

[[2]]
[1] 40 20 30
> dataanalyst[[1]]
[1] "flipkart"

"house-furniture" "snapdeal"

> dataanalyst[[2]][2]
[1] 20
Finaly we will have a look at the data frames. We create a data frame of students
and test scores.
> df1 <- data.frame (students = c("Ashim", "Jaya", "Raunak"), test1 = c(80,90,70) )
> str(df1)

'data.frame':
3 obs. of 2 variables:
$ students: Factor w/ 3 levels "Ashim","Jaya",..: 1 2 3
$ test1
: num 80 90 70
> df1$students <-as.character(df1$students)
> str(df1)

'data.frame':
3 obs. of 2 variables:
$ students: chr "Ashim" "Jaya" "Raunak"
$ test1
: num 80 90 70
> summary(df1$test1)
Min. 1st Qu.
70
75

Median
80

Mean 3rd Qu.


80
85

Max.
90

Now we see how to add a new variable to our data frame.


> df1<- cbind(df1, birth = as.Date(c("1990-08-01", "1995-07-23", "1993-12-13")))
> str(df1)
4

'data.frame':
3 obs. of 3 variables:
$ students: chr "Ashim" "Jaya" "Raunak"
$ test1
: num 80 90 70
$ birth
: Date, format: "1990-08-01" "1995-07-23" ...
> df1<-rbind(df1,c("Kaushik", 95, "1990-08-09"))
> df1

1
2
3
4

students test1
birth
Ashim
80 1990-08-01
Jaya
90 1995-07-23
Raunak
70 1993-12-13
Kaushik
95 1990-08-09

> head(df1)

1
2
3
4

students test1
birth
Ashim
80 1990-08-01
Jaya
90 1995-07-23
Raunak
70 1993-12-13
Kaushik
95 1990-08-09

> tail(df1)

1
2
3
4

students test1
birth
Ashim
80 1990-08-01
Jaya
90 1995-07-23
Raunak
70 1993-12-13
Kaushik
95 1990-08-09

This is the end of lesson one. We will discuss more in the subsequent lesson.

You might also like