Professional Documents
Culture Documents
"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
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,]
>
We will now see how the list works. For this, we consider the e-retailers for
thinks in our stores.
>
>
>
>
[[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
Max.
90
'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.