ls()
## character(0)
rm(list=ls())
gc()
##          used (Mb) gc trigger (Mb) max used (Mb)
## Ncells 366698 19.6     592000 31.7   460000 24.6
## Vcells 569072  4.4    1308461 10.0   888865  6.8
getwd()
## [1] "C:/Users/Dell/Documents/R/newproject"
pcm=c(90,72,81,89,82,NA,NA,75,70,86,NA,79,NA,94,NA,72,69)
mean(pcm)
## [1] NA
summary(pcm)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max.    NA's 
##   69.00   72.00   80.00   79.92   86.75   94.00       5
mean(pcm,na.rm = TRUE)
## [1] 79.91667
pcm2=na.omit(pcm)
pcm2
##  [1] 90 72 81 89 82 75 70 86 79 94 72 69
## attr(,"na.action")
## [1]  6  7 11 13 15
## attr(,"class")
## [1] "omit"
mean(pcm2)
## [1] 79.91667
is.na(pcm)
##  [1] FALSE FALSE FALSE FALSE FALSE  TRUE  TRUE FALSE FALSE FALSE  TRUE
## [12] FALSE  TRUE FALSE  TRUE FALSE FALSE
table(is.na(pcm))
## 
## FALSE  TRUE 
##    12     5
plot(density(pcm2))
is.na(pcm)=T
pcm3=ifelse(is.na(pcm),median(pcm,na.rm=T),pcm)
pcm3
##  [1] NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
summary(pcm)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max.    NA's 
##      NA      NA      NA     NaN      NA      NA      17
summary(pcm3)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max.    NA's 
##      NA      NA      NA     NaN      NA      NA      17
income= c("50,000","$10,00  0",20000,"30000",
          25000,"100,  000",60000,"$40000",
          "$50,000",90000,"50000$",
          "99  000$",40000,
          125000,"$45,00  0","$25,000")
mean(income)
## Warning in mean.default(income): argument is not numeric or logical:
## returning NA
## [1] NA
grepl(",",income)
##  [1]  TRUE  TRUE FALSE FALSE FALSE  TRUE FALSE FALSE  TRUE FALSE FALSE
## [12] FALSE FALSE FALSE  TRUE  TRUE
table(grepl(",",income))
## 
## FALSE  TRUE 
##    10     6
grepl("\\$",income)
##  [1] FALSE  TRUE FALSE FALSE FALSE FALSE FALSE  TRUE  TRUE FALSE  TRUE
## [12]  TRUE FALSE FALSE  TRUE  TRUE
table(grepl("\\$",income))
## 
## FALSE  TRUE 
##     9     7
length(income)
## [1] 16
sample(16,10,F)
##  [1]  4  2 16 13 10  1 11  5 14 15
b=sample(16,10,F)

income2=income[1:10]
income2=income[b]
income
##  [1] "50,000"    "$10,00  0" "20000"     "30000"     "25000"    
##  [6] "100,  000" "60000"     "$40000"    "$50,000"   "90000"    
## [11] "50000$"    "99  000$"  "40000"     "125000"    "$45,00  0"
## [16] "$25,000"
income2=gsub(",","",income)
income2
##  [1] "50000"    "$1000  0" "20000"    "30000"    "25000"    "100  000"
##  [7] "60000"    "$40000"   "$50000"   "90000"    "50000$"   "99  000$"
## [13] "40000"    "125000"   "$4500  0" "$25000"
income3=gsub("\\$","",income2)
income3
##  [1] "50000"    "1000  0"  "20000"    "30000"    "25000"    "100  000"
##  [7] "60000"    "40000"    "50000"    "90000"    "50000"    "99  000" 
## [13] "40000"    "125000"   "4500  0"  "25000"
income3=gsub(" ","",income3)
income3
##  [1] "50000"  "10000"  "20000"  "30000"  "25000"  "100000" "60000" 
##  [8] "40000"  "50000"  "90000"  "50000"  "99000"  "40000"  "125000"
## [15] "45000"  "25000"
income4=as.numeric(income3)
income4
##  [1]  50000  10000  20000  30000  25000 100000  60000  40000  50000  90000
## [11]  50000  99000  40000 125000  45000  25000
mean(income4)
## [1] 53687.5
ls()
## [1] "b"       "income"  "income2" "income3" "income4" "pcm"     "pcm2"   
## [8] "pcm3"
list=ls()
list
## [1] "b"       "income"  "income2" "income3" "income4" "pcm"     "pcm2"   
## [8] "pcm3"
list[c(-1,-5,-9)]
## [1] "income"  "income2" "income3" "pcm"     "pcm2"    "pcm3"
rm(list=list[c(-1,-5,-9)])
ls()
## [1] "b"       "income4" "list"
gc()
##          used (Mb) gc trigger (Mb) max used (Mb)
## Ncells 381277 20.4     750400 40.1   592000 31.7
## Vcells 601354  4.6    1308461 10.0  1076960  8.3
ls()
## [1] "b"       "income4" "list"
income4
##  [1]  50000  10000  20000  30000  25000 100000  60000  40000  50000  90000
## [11]  50000  99000  40000 125000  45000  25000
length(income4)
## [1] 16
income4[17]=200000
income4
##  [1]  50000  10000  20000  30000  25000 100000  60000  40000  50000  90000
## [11]  50000  99000  40000 125000  45000  25000 200000
income4[2]=200000
library(readr)
BigDiamonds <- read_csv("C:/Users/Dell/Downloads/BigDiamonds.csv (2)/BigDiamonds.csv")
## Warning: Missing column names filled in: 'X1' [1]
## Parsed with column specification:
## cols(
##   X1 = col_integer(),
##   carat = col_double(),
##   cut = col_character(),
##   color = col_character(),
##   clarity = col_character(),
##   table = col_double(),
##   depth = col_double(),
##   cert = col_character(),
##   measurements = col_character(),
##   price = col_integer(),
##   x = col_double(),
##   y = col_double(),
##   z = col_double()
## )
summary(BigDiamonds)
##        X1             carat           cut               color          
##  Min.   :     1   Min.   :0.200   Length:598024      Length:598024     
##  1st Qu.:149507   1st Qu.:0.500   Class :character   Class :character  
##  Median :299013   Median :0.900   Mode  :character   Mode  :character  
##  Mean   :299013   Mean   :1.071                                        
##  3rd Qu.:448518   3rd Qu.:1.500                                        
##  Max.   :598024   Max.   :9.250                                        
##                                                                        
##    clarity              table           depth           cert          
##  Length:598024      Min.   : 0.00   Min.   : 0.00   Length:598024     
##  Class :character   1st Qu.:56.00   1st Qu.:61.00   Class :character  
##  Mode  :character   Median :58.00   Median :62.10   Mode  :character  
##                     Mean   :57.63   Mean   :61.06                     
##                     3rd Qu.:59.00   3rd Qu.:62.70                     
##                     Max.   :75.90   Max.   :81.30                     
##                                                                       
##  measurements           price             x                y         
##  Length:598024      Min.   :  300   Min.   : 0.150   Min.   : 1.000  
##  Class :character   1st Qu.: 1220   1st Qu.: 4.740   1st Qu.: 4.970  
##  Mode  :character   Median : 3503   Median : 5.780   Median : 6.050  
##                     Mean   : 8753   Mean   : 5.991   Mean   : 6.199  
##                     3rd Qu.:11174   3rd Qu.: 6.970   3rd Qu.: 7.230  
##                     Max.   :99990   Max.   :13.890   Max.   :13.890  
##                     NA's   :713     NA's   :1815     NA's   :1852    
##        z         
##  Min.   : 0.040  
##  1st Qu.: 3.120  
##  Median : 3.860  
##  Mean   : 4.033  
##  3rd Qu.: 4.610  
##  Max.   :13.180  
##  NA's   :2544
table(BigDiamonds$color)
## 
##     D     E     F     G     H     I     J     K     L 
## 73630 93483 93573 96204 86619 70282 48709 25868  9656
Diamonds2=BigDiamonds[is.na(BigDiamonds$price),]
Diamonds3=BigDiamonds[is.na(BigDiamonds$price)==F,]
summary(Diamonds3)
##        X1             carat           cut               color          
##  Min.   :   494   Min.   :0.200   Length:597311      Length:597311     
##  1st Qu.:149842   1st Qu.:0.500   Class :character   Class :character  
##  Median :299309   Median :0.900   Mode  :character   Mode  :character  
##  Mean   :299277   Mean   :1.072                                        
##  3rd Qu.:448677   3rd Qu.:1.500                                        
##  Max.   :598024   Max.   :9.250                                        
##                                                                        
##    clarity              table           depth           cert          
##  Length:597311      Min.   : 0.00   Min.   : 0.00   Length:597311     
##  Class :character   1st Qu.:56.00   1st Qu.:61.00   Class :character  
##  Mode  :character   Median :58.00   Median :62.10   Mode  :character  
##                     Mean   :57.63   Mean   :61.06                     
##                     3rd Qu.:59.00   3rd Qu.:62.70                     
##                     Max.   :75.90   Max.   :81.30                     
##                                                                       
##  measurements           price             x                y         
##  Length:597311      Min.   :  300   Min.   : 0.150   Min.   : 1.000  
##  Class :character   1st Qu.: 1220   1st Qu.: 4.740   1st Qu.: 4.970  
##  Mode  :character   Median : 3503   Median : 5.780   Median : 6.050  
##                     Mean   : 8753   Mean   : 5.993   Mean   : 6.201  
##                     3rd Qu.:11174   3rd Qu.: 6.970   3rd Qu.: 7.230  
##                     Max.   :99990   Max.   :13.890   Max.   :13.890  
##                                     NA's   :1814     NA's   :1851    
##        z         
##  Min.   : 0.040  
##  1st Qu.: 3.120  
##  Median : 3.860  
##  Mean   : 4.035  
##  3rd Qu.: 4.610  
##  Max.   :13.180  
##  NA's   :2543
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
Diamonds21=select(Diamonds2,price,color,cut)
object.size(BigDiamonds)
## 74738272 bytes
object.size(Diamonds21)
## 15856 bytes
library(Hmisc)
## Loading required package: lattice
## Loading required package: survival
## Loading required package: Formula
## Loading required package: ggplot2
## 
## Attaching package: 'Hmisc'
## The following objects are masked from 'package:dplyr':
## 
##     combine, src, summarize
## The following objects are masked from 'package:base':
## 
##     format.pval, round.POSIXt, trunc.POSIXt, units

summarize(Diamonds3$price,Diamonds3$color,median)
##   Diamonds3$color Diamonds3$price
## 1               D            2690
## 2               E            2342
## 3               F            2966
## 4               G            3720
## 5               H            4535
## 6               I            4717
## 7               J            4697
## 8               K            4418
## 9               L            3017
d=summarize(Diamonds3$price,Diamonds3$color,median)
d
##   Diamonds3$color Diamonds3$price
## 1               D            2690
## 2               E            2342
## 3               F            2966
## 4               G            3720
## 5               H            4535
## 6               I            4717
## 7               J            4697
## 8               K            4418
## 9               L            3017
names(d)=c("color","price")
d
##   color price
## 1     D  2690
## 2     E  2342
## 3     F  2966
## 4     G  3720
## 5     H  4535
## 6     I  4717
## 7     J  4697
## 8     K  4418
## 9     L  3017
getwd()
## [1] "C:/Users/Dell/Documents/R/newproject"
write.csv(d,file="medians.csv")
head(Diamonds21)
## # A tibble: 6 × 3
##   price color    cut
##   <int> <chr>  <chr>
## 1    NA     K V.Good
## 2    NA     G   Good
## 3    NA     J   Good
## 4    NA     D V.Good
## 5    NA     K V.Good
## 6    NA     G   Good
Diamonds21$newprice=NULL
Diamonds21$newprice=ifelse(Diamonds21$color=="J",4697,Diamonds21$price)

Diamonds21$newprice<-ifelse(Diamonds21$color=="D",2690,Diamonds21$newprice)
Diamonds21$newprice<-ifelse(Diamonds21$color=="E",2342,Diamonds21$newprice)
Diamonds21$newprice<-ifelse(Diamonds21$color=="F",2966,Diamonds21$newprice)
Diamonds21$newprice<-ifelse(Diamonds21$color=="G",3720,Diamonds21$newprice)
Diamonds21$newprice<-ifelse(Diamonds21$color=="H",4535,Diamonds21$newprice)
Diamonds21$newprice<-ifelse(Diamonds21$color=="I",4717,Diamonds21$newprice)
Diamonds21$newprice<-ifelse(Diamonds21$color=="K",4418,Diamonds21$newprice)
Diamonds21$newprice<-ifelse(Diamonds21$color=="L",3017,Diamonds21$newprice)

Diamonds21$price=ifelse(Diamonds21$color==d$color,d$price,Diamonds21$price)
## Warning in Diamonds21$color == d$color: longer object length is not a
## multiple of shorter object length
summary(Diamonds21)
##      price         color               cut               newprice   
##  Min.   :2342   Length:713         Length:713         Min.   :2342  
##  1st Qu.:2966   Class :character   Class :character   1st Qu.:2966  
##  Median :3720   Mode  :character   Mode  :character   Median :3720  
##  Mean   :3799                                         Mean   :3595  
##  3rd Qu.:4535                                         3rd Qu.:4535  
##  Max.   :4717                                         Max.   :4717  
##  NA's   :633
summarize(Diamonds21$newprice,Diamonds21$color,median)
##   Diamonds21$color Diamonds21$newprice
## 1                D                2690
## 2                E                2342
## 3                F                2966
## 4                G                3720
## 5                H                4535
## 6                I                4717
## 7                J                4697
## 8                K                4418
## 9                L                3017
head(Diamonds21)
## # A tibble: 6 × 4
##   price color    cut newprice
##   <dbl> <chr>  <chr>    <dbl>
## 1    NA     K V.Good     4418
## 2    NA     G   Good     3720
## 3    NA     J   Good     4697
## 4    NA     D V.Good     2690
## 5    NA     K V.Good     4418
## 6    NA     G   Good     3720
table(is.na(Diamonds21$color))
## 
## FALSE 
##   713
summary(Diamonds21)
##      price         color               cut               newprice   
##  Min.   :2342   Length:713         Length:713         Min.   :2342  
##  1st Qu.:2966   Class :character   Class :character   1st Qu.:2966  
##  Median :3720   Mode  :character   Mode  :character   Median :3720  
##  Mean   :3799                                         Mean   :3595  
##  3rd Qu.:4535                                         3rd Qu.:4535  
##  Max.   :4717                                         Max.   :4717  
##  NA's   :633
summarize(Diamonds3$price,llist(Diamonds3$color,Diamonds3$cut),median)
##    Diamonds3$color Diamonds3$cut Diamonds3$price
## 1                D          Good          1970.0
## 2                D         Ideal          3021.5
## 3                D        V.Good          2330.0
## 4                E          Good          1821.0
## 5                E         Ideal          2455.0
## 6                E        V.Good          2395.0
## 7                F          Good          2027.0
## 8                F         Ideal          3391.0
## 9                F        V.Good          2800.0
## 10               G          Good          2324.0
## 11               G         Ideal          4353.0
## 12               G        V.Good          3363.0
## 13               H          Good          2756.0
## 14               H         Ideal          5755.0
## 15               H        V.Good          3557.0
## 16               I          Good          2406.0
## 17               I         Ideal          6126.0
## 18               I        V.Good          3612.5
## 19               J          Good          2458.0
## 20               J         Ideal          5771.0
## 21               J        V.Good          3462.0
## 22               K          Good          2044.0
## 23               K         Ideal          5793.0
## 24               K        V.Good          3480.0
## 25               L          Good          1550.0
## 26               L         Ideal          3998.5
## 27               L        V.Good          2239.0
e=summarize(Diamonds3$price,llist(Diamonds3$color,Diamonds3$cut),median)

e
##    Diamonds3$color Diamonds3$cut Diamonds3$price
## 1                D          Good          1970.0
## 2                D         Ideal          3021.5
## 3                D        V.Good          2330.0
## 4                E          Good          1821.0
## 5                E         Ideal          2455.0
## 6                E        V.Good          2395.0
## 7                F          Good          2027.0
## 8                F         Ideal          3391.0
## 9                F        V.Good          2800.0
## 10               G          Good          2324.0
## 11               G         Ideal          4353.0
## 12               G        V.Good          3363.0
## 13               H          Good          2756.0
## 14               H         Ideal          5755.0
## 15               H        V.Good          3557.0
## 16               I          Good          2406.0
## 17               I         Ideal          6126.0
## 18               I        V.Good          3612.5
## 19               J          Good          2458.0
## 20               J         Ideal          5771.0
## 21               J        V.Good          3462.0
## 22               K          Good          2044.0
## 23               K         Ideal          5793.0
## 24               K        V.Good          3480.0
## 25               L          Good          1550.0
## 26               L         Ideal          3998.5
## 27               L        V.Good          2239.0
names(e)=c("color","price")
e
##    color  price     NA
## 1      D   Good 1970.0
## 2      D  Ideal 3021.5
## 3      D V.Good 2330.0
## 4      E   Good 1821.0
## 5      E  Ideal 2455.0
## 6      E V.Good 2395.0
## 7      F   Good 2027.0
## 8      F  Ideal 3391.0
## 9      F V.Good 2800.0
## 10     G   Good 2324.0
## 11     G  Ideal 4353.0
## 12     G V.Good 3363.0
## 13     H   Good 2756.0
## 14     H  Ideal 5755.0
## 15     H V.Good 3557.0
## 16     I   Good 2406.0
## 17     I  Ideal 6126.0
## 18     I V.Good 3612.5
## 19     J   Good 2458.0
## 20     J  Ideal 5771.0
## 21     J V.Good 3462.0
## 22     K   Good 2044.0
## 23     K  Ideal 5793.0
## 24     K V.Good 3480.0
## 25     L   Good 1550.0
## 26     L  Ideal 3998.5
## 27     L V.Good 2239.0
getwd()
## [1] "C:/Users/Dell/Documents/R/newproject"
write.csv(e,file="medianscuts.csv")

Diamonds21$newprice2=ifelse(Diamonds21$color=="D"&Diamonds21$cut=="Good",1970,Diamonds21$price)
Diamonds21$newprice2=ifelse(Diamonds21$color=="D"&Diamonds21$cut=="Ideal",3021.5,Diamonds21$newprice2)
Diamonds21$newprice2=ifelse(Diamonds21$color=="D"&Diamonds21$cut=="V.Good",2330,Diamonds21$newprice2)
Diamonds21$newprice2=ifelse(Diamonds21$color=="E"&Diamonds21$cut=="Good",1821,Diamonds21$newprice2)
Diamonds21$newprice2=ifelse(Diamonds21$color=="E"&Diamonds21$cut=="Ideal",2455,Diamonds21$newprice2)
Diamonds21$newprice2=ifelse(Diamonds21$color=="E"&Diamonds21$cut=="V.Good",2395,Diamonds21$newprice2)
Diamonds21$newprice2=ifelse(Diamonds21$color=="F"&Diamonds21$cut=="Good",2027,Diamonds21$newprice2)
Diamonds21$newprice2=ifelse(Diamonds21$color=="F"&Diamonds21$cut=="Ideal",3391,Diamonds21$newprice2)
Diamonds21$newprice2=ifelse(Diamonds21$color=="F"&Diamonds21$cut=="V.Good",2800,Diamonds21$newprice2)
Diamonds21$newprice2=ifelse(Diamonds21$color=="G"&Diamonds21$cut=="Good",2324,Diamonds21$newprice2)
Diamonds21$newprice2=ifelse(Diamonds21$color=="G"&Diamonds21$cut=="Ideal",4353,Diamonds21$newprice2)
Diamonds21$newprice2=ifelse(Diamonds21$color=="G"&Diamonds21$cut=="V.Good",3363,Diamonds21$newprice2)
Diamonds21$newprice2=ifelse(Diamonds21$color=="H"&Diamonds21$cut=="Good",2756,Diamonds21$newprice2)
Diamonds21$newprice2=ifelse(Diamonds21$color=="H"&Diamonds21$cut=="Ideal",5755,Diamonds21$newprice2)
Diamonds21$newprice2=ifelse(Diamonds21$color=="H"&Diamonds21$cut=="V.Good",3557,Diamonds21$newprice2)
Diamonds21$newprice2=ifelse(Diamonds21$color=="I"&Diamonds21$cut=="Good",2406,Diamonds21$newprice2)
Diamonds21$newprice2=ifelse(Diamonds21$color=="I"&Diamonds21$cut=="Ideal",6126,Diamonds21$newprice2)
Diamonds21$newprice2=ifelse(Diamonds21$color=="I"&Diamonds21$cut=="V.Good",3612.5,Diamonds21$newprice2)
Diamonds21$newprice2=ifelse(Diamonds21$color=="J"&Diamonds21$cut=="Good",2458,Diamonds21$newprice2)
Diamonds21$newprice2=ifelse(Diamonds21$color=="J"&Diamonds21$cut=="Ideal",5771,Diamonds21$newprice2)
Diamonds21$newprice2=ifelse(Diamonds21$color=="J"&Diamonds21$cut=="V.Good",3462,Diamonds21$newprice2)
Diamonds21$newprice2=ifelse(Diamonds21$color=="K"&Diamonds21$cut=="Good",2044,Diamonds21$newprice2)
Diamonds21$newprice2=ifelse(Diamonds21$color=="K"&Diamonds21$cut=="Ideal",5793,Diamonds21$newprice2)
Diamonds21$newprice2=ifelse(Diamonds21$color=="K"&Diamonds21$cut=="V.Good",3480,Diamonds21$newprice2)
Diamonds21$newprice2=ifelse(Diamonds21$color=="L"&Diamonds21$cut=="Good",1550,Diamonds21$newprice2)
Diamonds21$newprice2=ifelse(Diamonds21$color=="L"&Diamonds21$cut=="Ideal",3998.5,Diamonds21$newprice2)
Diamonds21$newprice2=ifelse(Diamonds21$color=="L"&Diamonds21$cut=="V.Good",2239,Diamonds21$newprice2)


summarize(Diamonds21$newprice2,llist(Diamonds21$cut,Diamonds21$color),median)
##    Diamonds21$cut Diamonds21$color Diamonds21$newprice2
## 1            Good                D               1970.0
## 2            Good                E               1821.0
## 3            Good                F               2027.0
## 4            Good                G               2324.0
## 5            Good                H               2756.0
## 6            Good                I               2406.0
## 7            Good                J               2458.0
## 8            Good                K               2044.0
## 9            Good                L               1550.0
## 10          Ideal                D               3021.5
## 11          Ideal                E               2455.0
## 12          Ideal                F               3391.0
## 13          Ideal                G               4353.0
## 14          Ideal                H               5755.0
## 15          Ideal                I               6126.0
## 16          Ideal                J               5771.0
## 17          Ideal                K               5793.0
## 18         V.Good                D               2330.0
## 19         V.Good                E               2395.0
## 20         V.Good                F               2800.0
## 21         V.Good                G               3363.0
## 22         V.Good                H               3557.0
## 23         V.Good                I               3612.5
## 24         V.Good                J               3462.0
## 25         V.Good                K               3480.0
## 26         V.Good                L               2239.0
Diamonds21
## # A tibble: 713 × 5
##    price color    cut newprice newprice2
##    <dbl> <chr>  <chr>    <dbl>     <dbl>
## 1     NA     K V.Good     4418      3480
## 2     NA     G   Good     3720      2324
## 3     NA     J   Good     4697      2458
## 4     NA     D V.Good     2690      2330
## 5     NA     K V.Good     4418      3480
## 6     NA     G   Good     3720      2324
## 7     NA     G   Good     3720      2324
## 8     NA     D V.Good     2690      2330
## 9     NA     K V.Good     4418      3480
## 10    NA     F   Good     2966      2027
## # ... with 703 more rows
attach(Diamonds21)
boxplot(BigDiamonds$price~BigDiamonds$color)

hist(BigDiamonds$price)

barplot(BigDiamonds$price)

#plot(density(BigDiamonds$price))

data(anscombe)
lapply(anscombe,mean)
## $x1
## [1] 9
## 
## $x2
## [1] 9
## 
## $x3
## [1] 9
## 
## $x4
## [1] 9
## 
## $y1
## [1] 7.500909
## 
## $y2
## [1] 7.500909
## 
## $y3
## [1] 7.5
## 
## $y4
## [1] 7.500909
lapply(anscombe,sd)
## $x1
## [1] 3.316625
## 
## $x2
## [1] 3.316625
## 
## $x3
## [1] 3.316625
## 
## $x4
## [1] 3.316625
## 
## $y1
## [1] 2.031568
## 
## $y2
## [1] 2.031657
## 
## $y3
## [1] 2.030424
## 
## $y4
## [1] 2.030579
data(mtcars)
table(mtcars$mpg)
## 
## 10.4 13.3 14.3 14.7   15 15.2 15.5 15.8 16.4 17.3 17.8 18.1 18.7 19.2 19.7 
##    2    1    1    1    1    2    1    1    1    1    1    1    1    2    1 
##   21 21.4 21.5 22.8 24.4   26 27.3 30.4 32.4 33.9 
##    2    2    1    2    1    1    1    2    1    1
table(mtcars$cyl)
## 
##  4  6  8 
## 11  7 14
table(mtcars$gear)
## 
##  3  4  5 
## 15 12  5
table(mtcars$cyl,mtcars$gear)
##    
##      3  4  5
##   4  1  8  2
##   6  2  4  1
##   8 12  0  2
library(Hmisc)
summarize(mtcars$mpg,llist(mtcars$cyl,mtcars$gear),mean)
##   mtcars$cyl mtcars$gear mtcars$mpg
## 1          4           3     21.500
## 2          4           4     26.925
## 3          4           5     28.200
## 4          6           3     19.750
## 5          6           4     19.750
## 6          6           5     19.700
## 7          8           3     15.050
## 8          8           5     15.400
with(mtcars, tapply(mpg, list(cyl,gear), FUN= mean))
##       3      4    5
## 4 21.50 26.925 28.2
## 6 19.75 19.750 19.7
## 8 15.05     NA 15.4
library(reshape2)
acast(mtcars, cyl~gear, value.var='mpg', mean)
##       3      4    5
## 4 21.50 26.925 28.2
## 6 19.75 19.750 19.7
## 8 15.05    NaN 15.4