Data science Software Course Training in Ameerpet Hyderabad

Data science Software Course Training in Ameerpet Hyderabad

Thursday, 30 March 2017

R and Analytics Basics 2

sankara.deva2016@gmail.com

bharat sreeram , Ventech It Solutions
-----------------------------------------------------------------------------------------------

> info =

  read.csv("C:/Users/Hadoop/Desktop/info.txt")
> info
   id    name   sal sex city
1 101    Amar 40000   m  hyd
2 102   Amala 50000   f  del
3 103   sunil 70000   m  hyd
4 104 sunitha 80000   f  hyd
5 105  ankith 90000   m  del
6 106 ankitha 60000   f  hyd
>
> class(info)
[1] "data.frame"
> str(info)
 -- structure of info.
'data.frame':   6 obs. of  5 variables:
 $ id  : int  101 102 103 104 105 106
 $ name: Factor w/ 6 levels "Amala","Amar",..: 2 1 5 6 3 4
 $ sal : int  40000 50000 70000 80000 90000 60000
 $ sex : Factor w/ 2 levels "f","m": 2 1 2 1 2 1
 $ city: Factor w/ 2 levels "del","hyd": 2 1 2 2 1 2
>
> info$name
[1] Amar    Amala   sunil   sunitha ankith  ankitha
Levels: Amala Amar ankith ankitha sunil sunitha
> info$sal
[1] 40000 50000 70000 80000 90000 60000
>
-------------------
Updating Column:
 info$sal = info$sal+1000
Generating new Field for Data frame.
 info$tax = info$sal*10/100
  # conditional transformation,.
 info$grade = "C"
 info$grade[info$sal>=50000 & info$sal<=80000]    = "B"
 info$grade[info$sal>80000]="A"
> info
   id    name   sal sex city  tax grade
1 101    Amar 41000   m  hyd 4100     C
2 102   Amala 51000   f  del 5100     B
3 103   sunil 71000   m  hyd 7100     B
4 104 sunitha 81000   f  hyd 8100     A
5 105  ankith 91000   m  del 9100     A
6 106 ankitha 61000   f  hyd 6100     B
>
Grouping Aggregations:
--------------------------------
 res1 =
   aggregate( sal ~ sex, data=info, FUN=sum)
> res1
  sex    sal
1   f 193000
2   m 203000
>

 res2 =
   aggregate( sal ~ sex, data=info, FUN=mean)
> res2
  sex      sal
1   f 64333.33
2   m 67666.67

  res3 =
   aggregate( sal ~ sex, data=info, FUN=max)
> res3
  sex   sal
1   f 81000
2   m 91000

 res4 =
   aggregate( sal ~ sex, data=info, FUN=min)
> res4
  sex   sal
1   f 51000
2   m 41000


 res5 =
   aggregate( sal ~ sex, data=info, FUN=length)
> res5
  sex sal
1   f   3
2   m   3

---------------------------
 res6 = aggregate(sal ~ grade, data=info,
   FUN = length)
> res6
  grade sal
1     A   2
2     B   3
3     C   1

-----------------------------------
  select sex, grade, sum(sal) from info
    group by sex, grade;
Grouping by multiple columns of data frame.
 res7 =
   aggregate( sal ~ sex + grade, data=info, FUN=sum)
> res7
  sex grade    sal
1   f     A  81000
2   m     A  91000
3   f     B 112000
4   m     B  71000
5   m     C  41000

-----------------------------------------

 select sex, sum(sal), max(sal),
      min(sal), avg(sal), count(*)
   from info group by sex;
# performing multiple aggregation,
   of each data group.

Performing Multiple Aggregations:
r1 = aggregate(sal ~ sex , data=info , FUN=sum)
r2 = aggregate(sal ~ sex , data=info , FUN=max)
r3 = aggregate(sal ~ sex , data=info , FUN=min)
r4 = aggregate(sal ~ sex , data=info , FUN=mean)
r5 = aggregate(sal ~ sex , data=info , FUN=length)
resall = data.frame(sex=r1$sex,
          tot=r1$sal, avg=r4$sal,
         max= r2$sal, min=r3$sal,
        cnt=r5$sal)
> resall
  sex    tot      avg   max   min cnt
1   f 193000 64333.33 81000 51000   3
2   m 203000 67666.67 91000 41000   3
>
--------------------------------------
Working With Matrices.
 v = 1:15
  m1 = matrix(v, nrow=3)
 m2 = matrix(v, nrow=3 ,byrow=T)
 m3 = matrix(v, ncol=5, byrow=T)
 m4 = matrix(1:12,4)
 Operations of matrices.
 m = matrix(c(10,20,30,40), nrow=2)
 mx = matrix(c(12,34,56,23), nrow=2)
 r1 = m + mx   # element to element sum
 r2 = m * mx   #   "        "       multiplication
 r3 = m %*% mx  # matrix multiplication
 r4 = solve(m) # inverse of matrix.
 r5 = t(m) # transpose of matrix.
-------------------------------------------




























----------------------




























































1 comment:

  1. Good Post! Thank you so much for sharing this pretty post, it was so good to read and useful to improve my knowledge as updated one, keep blogging.
    Big Data Hadoop Training in electronic city

    ReplyDelete