#create a new function
mySum<-function(x,y)
 {
  return(x+y)
}

mySum(3,4)
#[1] 7

#get body of a function - just type the name of the function
mySum
# function(x,y)
# {
#   return(x+y)
# }








#special function to apply any function to multiple rows and columns at once

x<-data.frame(x1=seq(1,10,1),x2=2,x3=5)
x
#    x1 x2 x3
# 1   1  2  5
# 2   2  2  5
# 3   3  2  5
# 4   4  2  5
# 5   5  2  5
# 6   6  2  5
# 7   7  2  5
# 8   8  2  5
# 9   9  2  5
# 10 10  2  5

#apply function by column
apply(x,2,mean)
#  x1  x2  x3
# 5.5 2.0 5.0

#apply function by row
apply(x,1,mean)
# [1] 2.666667 3.000000 3.333333 3.666667 4.000000 4.333333 4.666667 5.000000 5.333333 5.666667

age=seq(11,20,1)
age
#[1] 11 12 13 14 15 16 17 18 19 20
m_name=letters[1:10]
m_name
# [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j"

rbind(m_name,age)

#        [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
# m_name "a"  "b"  "c"  "d"  "e"  "f"  "g"  "h"  "i"  "j" 
# age    "11" "12" "13" "14" "15" "16" "17" "18" "19" "20"

age=seq(11,20,1)
age
# [1] 11 12 13 14 15 16 17 18 19 20
m_name=letters[1:10]
m_name
# [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j"

cbind(m_name,age)
#       m_name age
#  [1,] "a"    "11"
#  [2,] "b"    "12"
#  [3,] "c"    "13"
#  [4,] "d"    "14"
#  [5,] "e"    "15"
#  [6,] "f"    "16"
#  [7,] "g"    "17"
#  [8,] "h"    "18"
#  [9,] "i"    "19"
# [10,] "j"    "20"

#read the data from the CSV available on URL
data2<-read.csv('https://vincentarelbundock.github.io/Rdatasets/csv/car/UN.csv')

data3<-na.omit(data2)

nrow(data2)
#[1] 207
nrow(data3)
#[1] 193

#### use datasets from library MASS

library('MASS')

### find frequency based on different column value
table(Cars93$Type)

# Compact   Large Midsize   Small  Sporty     Van
#      16      11      22      21      14       9


### subset data based on a column value
Cars99A<-subset(Cars93,Type=='Small')

table(Cars99A$Type)

# Compact   Large Midsize   Small  Sporty     Van
#       0       0       0      21       0       0


x<-c(2,1,3,5,9,8,6,7)
x
#[1] 2 1 3 5 9 8 6 7


###
sort(x)
#[1] 1 2 3 5 6 7 8 9

sort(x,decreasing = T)
#[1] 9 8 7 6 5 3 2 1

## sorting an object based a column value
Cars93[order(Cars93$Type),]

## try sorting an object based multiple column values

table(Cars93$AirBags)

#Driver & Passenger        Driver only               None
#                16                 43                 34


#tabulate for multiple variables
table(Cars93$AirBags,Cars93$Type)

#                    Compact Large Midsize Small Sporty Van
# Driver & Passenger       2     4       7     0      3   0
# Driver only              9     7      11     5      8   3
# None                     5     0       4    16      3   6
#

sort(Cars93$Min.Price,decreasing = T)

#log transformation
library('MASS')

#Log transformation
 Cars93$Min.Price

log(Cars93$Min.Price)



#square transformation
Cars93$Min.Price*Cars93$Min.Price

Comments