ls()
## character(0)
rm(list=ls())
gc()
##          used (Mb) gc trigger (Mb) max used (Mb)
## Ncells 290539  7.8     460000 12.3   350000  9.4
## Vcells 320228  2.5     786432  6.0   677388  5.2
library(data.table)

getwd()
## [1] "C:/Users/dell/Desktop"
setwd("C:/Users/dell/Desktop")
dir()
##  [1] "1.png"                                                              
##  [2] "2.png"                                                              
##  [3] "5128OS_09_01.jpg"                                                   
##  [4] "adult.data.txt"                                                     
##  [5] "airline.sas7bdat"                                                   
##  [6] "Analytics decisionstats.com Audience Overview 20110617-20120717.csv"
##  [7] "basicR.html"                                                        
##  [8] "basicR.R"                                                           
##  [9] "BigDiamonds.csv"                                                    
## [10] "BigDiamonds.csv.zip"                                                
## [11] "Boston.csv"                                                         
## [12] "Cars.sav"                                                           
## [13] "casestudy"                                                          
## [14] "ccFraud.csv"                                                        
## [15] "Certificate Doc.docx"                                               
## [16] "CmapServer Download _ Cmap.html"                                    
## [17] "CmapServer Download _ Cmap_files"                                   
## [18] "Coxcombs.jpg"                                                       
## [19] "cricketparsing.R"                                                   
## [20] "data input.R"                                                       
## [21] "data_input.html"                                                    
## [22] "datatable"                                                          
## [23] "datatablerevised.R"                                                 
## [24] "datatablerevised.spin.R"                                            
## [25] "datatablerevised.spin.Rmd"                                          
## [26] "day8 session 4.fbr"                                                 
## [27] "day9 session 1.fbr"                                                 
## [28] "desktop.ini"                                                        
## [29] "Dropbox.lnk"                                                        
## [30] "exam.html"                                                          
## [31] "exam.R"                                                             
## [32] "ie_data.xls"                                                        
## [33] "lastsave.txt"                                                       
## [34] "lastsave2"                                                          
## [35] "library.docx"                                                       
## [36] "Minard.png"                                                         
## [37] "modules"                                                            
## [38] "modules.zip"                                                        
## [39] "my first code.R"                                                    
## [40] "mycode.docx"                                                        
## [41] "mycode.html"                                                        
## [42] "mycode.R"                                                           
## [43] "myfirstRcode.R"                                                     
## [44] "New folder"                                                         
## [45] "New Folder (2)"                                                     
## [46] "new1"                                                               
## [47] "Quiz 1 R.docx"                                                      
## [48] "rfmanalysis2.html"                                                  
## [49] "rfmanalysis2.R"                                                     
## [50] "rsconnect"                                                          
## [51] "SnowMap_Points.png"                                                 
## [52] "test.csv"                                                           
## [53] "Untitled (3).wma"                                                   
## [54] "Untitled (3).wma.wav"                                               
## [55] "Untitled 88.wma"
diamonds=fread("BigDiamonds.csv")
## 
Read 41.8% of 598024 rows
Read 61.9% of 598024 rows
Read 80.3% of 598024 rows
Read 97.0% of 598024 rows
Read 598024 rows and 13 (of 13) columns from 0.049 GB file in 00:00:06
tables()
##      NAME        NROW NCOL MB
## [1,] diamonds 598,024   13 75
##      COLS                                                                
## [1,] V1,carat,cut,color,clarity,table,depth,cert,measurements,price,x,y,z
##      KEY
## [1,]    
## Total: 75MB
setkey(diamonds,V1)
head(diamonds,10)
##         V1 carat    cut color clarity table depth cert       measurements
##  1:      1  0.25 V.Good     K      I1    59  63.7  GIA 3.96 x 3.95 x 2.52
##  2:     10  0.20   Good     F     SI1    65  54.9  GIA 3.83 x 4.00 x 2.14
##  3:    100  0.24 V.Good     I     SI1    57  63.0  IGI 3.96 x 4.00 x 2.50
##  4:   1000  0.29 V.Good     D     SI2    58  63.7  GIA 4.19 x 4.16 x 2.66
##  5:  10000  0.40  Ideal     J     SI2    59  62.6  IGI 4.68 x 4.72 x 2.94
##  6: 100000  0.30  Ideal     E    VVS2    59  60.4  GIA 4.33 x 4.31 x 2.61
##  7: 100001  0.30  Ideal     E    VVS2    56  62.3  GIA 4.28 x 4.26 x 2.66
##  8: 100002  0.41   Good     E     VS1    65  59.3  GIA 4.71 x 4.92 x 2.85
##  9: 100003  0.33 V.Good     E    VVS2    58  62.6  GIA 4.39 x 4.36 x 2.74
## 10: 100004  0.31  Ideal     G    VVS2    57  61.7  GIA 4.35 x 4.34 x 2.68
##     price    x    y    z
##  1:    NA 3.96 3.95 2.52
##  2:    NA 3.83 4.00 2.14
##  3:    NA 3.96 4.00 2.50
##  4:   340 4.19 4.16 2.66
##  5:   493 4.68 4.72 2.94
##  6:   890 4.33 4.31 2.61
##  7:   890 4.28 4.26 2.66
##  8:   890 4.71 4.92 2.85
##  9:   890 4.39 4.36 2.74
## 10:   890 4.35 4.34 2.68
#give me average price for all diamonds with color J

diamonds[color=="J",mean(price,na.rm = T),]
## [1] 9423.581
diamonds[,mean(price,na.rm = T),color]
##    color       V1
## 1:     K 9694.257
## 2:     F 8234.730
## 3:     I 9541.319
## 4:     D 8266.346
## 5:     J 9423.581
## 6:     E 7282.990
## 7:     G 8984.200
## 8:     H 9941.795
## 9:     L 7109.228
#give me average price and carat size for  diamonds >2 carat 
#grouped by cut and color

diamonds[carat>2,.(.N,mean(price,na.rm = T),mean(carat,na.rm = T)),.(cut,color)]
##        cut color     N       V2       V3
##  1:   Good     D   246 31198.40 2.404878
##  2: V.Good     K  1915 26307.26 2.988731
##  3:  Ideal     F  6235 37875.75 2.496457
##  4:   Good     G   729 29543.15 2.525514
##  5:  Ideal     G  8834 34831.33 2.555443
##  6: V.Good     J  2684 28808.93 2.940697
##  7: V.Good     G  3140 32462.86 2.607363
##  8: V.Good     E  1727 34536.84 2.420376
##  9:   Good     H   826 28792.20 2.661768
## 10:   Good     J   604 27417.58 2.951374
## 11: V.Good     D  1178 34314.22 2.439414
## 12:   Good     I   779 26607.77 2.796624
## 13: V.Good     I  3462 27380.26 2.649792
## 14:  Ideal     J  7736 27902.59 2.788342
## 15:   Good     K   482 26264.28 2.950187
## 16:  Ideal     H 11678 33096.62 2.626627
## 17:  Ideal     K  5329 23929.88 2.774278
## 18: V.Good     H  3619 28889.99 2.626209
## 19:   Good     F   543 33422.36 2.590424
## 20:  Ideal     E  4287 40712.74 2.420217
## 21:   Good     E   351 29781.27 2.437778
## 22:  Ideal     L  1393 21266.51 2.889275
## 23: V.Good     F  2332 32818.06 2.512959
## 24: V.Good     L   590 24626.25 3.152237
## 25:   Good     L   204 25435.75 3.303529
## 26:  Ideal     I 10507 28943.56 2.637584
## 27:  Ideal     D  2938 41906.17 2.375524
##        cut color     N       V2       V3