Data Declaration

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
data() 
data = Orange 
View (data) 
class(data) 
## [1] "nfnGroupedData" "nfGroupedData"  "groupedData"    "data.frame"
str(data)
## Classes 'nfnGroupedData', 'nfGroupedData', 'groupedData' and 'data.frame':   35 obs. of  3 variables:
##  $ Tree         : Ord.factor w/ 5 levels "3"<"1"<"5"<"2"<..: 2 2 2 2 2 2 2 4 4 4 ...
##  $ age          : num  118 484 664 1004 1231 ...
##  $ circumference: num  30 58 87 115 120 142 145 33 69 111 ...
##  - attr(*, "formula")=Class 'formula'  language circumference ~ age | Tree
##   .. ..- attr(*, ".Environment")=<environment: R_EmptyEnv> 
##  - attr(*, "labels")=List of 2
##   ..$ x: chr "Time since December 31, 1968"
##   ..$ y: chr "Trunk circumference"
##  - attr(*, "units")=List of 2
##   ..$ x: chr "(days)"
##   ..$ y: chr "(mm)"

Summarise data

jumlah_circumference = summarise (data, sum(circumference))
jumlah_circumference
##   sum(circumference)
## 1               4055
jumlah = data %>%
  summarise(sum(circumference))
jumlah
##   sum(circumference)
## 1               4055

Arrange data

arrange_data = arrange(data, desc(age))
arrange_data
##    Tree  age circumference
## 1     1 1582           145
## 2     2 1582           203
## 3     3 1582           140
## 4     4 1582           214
## 5     5 1582           177
## 6     1 1372           142
## 7     2 1372           203
## 8     3 1372           139
## 9     4 1372           209
## 10    5 1372           174
## 11    1 1231           120
## 12    2 1231           172
## 13    3 1231           115
## 14    4 1231           179
## 15    5 1231           142
## 16    1 1004           115
## 17    2 1004           156
## 18    3 1004           108
## 19    4 1004           167
## 20    5 1004           125
## 21    1  664            87
## 22    2  664           111
## 23    3  664            75
## 24    4  664           112
## 25    5  664            81
## 26    1  484            58
## 27    2  484            69
## 28    3  484            51
## 29    4  484            62
## 30    5  484            49
## 31    1  118            30
## 32    2  118            33
## 33    3  118            30
## 34    4  118            32
## 35    5  118            30
head(arrange_data)
##   Tree  age circumference
## 1    1 1582           145
## 2    2 1582           203
## 3    3 1582           140
## 4    4 1582           214
## 5    5 1582           177
## 6    1 1372           142
susun = data %>%
  arrange(desc(age))
susun
##    Tree  age circumference
## 1     1 1582           145
## 2     2 1582           203
## 3     3 1582           140
## 4     4 1582           214
## 5     5 1582           177
## 6     1 1372           142
## 7     2 1372           203
## 8     3 1372           139
## 9     4 1372           209
## 10    5 1372           174
## 11    1 1231           120
## 12    2 1231           172
## 13    3 1231           115
## 14    4 1231           179
## 15    5 1231           142
## 16    1 1004           115
## 17    2 1004           156
## 18    3 1004           108
## 19    4 1004           167
## 20    5 1004           125
## 21    1  664            87
## 22    2  664           111
## 23    3  664            75
## 24    4  664           112
## 25    5  664            81
## 26    1  484            58
## 27    2  484            69
## 28    3  484            51
## 29    4  484            62
## 30    5  484            49
## 31    1  118            30
## 32    2  118            33
## 33    3  118            30
## 34    4  118            32
## 35    5  118            30

Filter data

filter_data = filter(data, Tree == '1')
filter_data
##   Tree  age circumference
## 1    1  118            30
## 2    1  484            58
## 3    1  664            87
## 4    1 1004           115
## 5    1 1231           120
## 6    1 1372           142
## 7    1 1582           145
saring = data %>%
  filter(Tree == '1')
saring
##   Tree  age circumference
## 1    1  118            30
## 2    1  484            58
## 3    1  664            87
## 4    1 1004           115
## 5    1 1231           120
## 6    1 1372           142
## 7    1 1582           145

Mutate data

mutate_data = mutate(data, age/12)
mutate_data
##    Tree  age circumference     age/12
## 1     1  118            30   9.833333
## 2     1  484            58  40.333333
## 3     1  664            87  55.333333
## 4     1 1004           115  83.666667
## 5     1 1231           120 102.583333
## 6     1 1372           142 114.333333
## 7     1 1582           145 131.833333
## 8     2  118            33   9.833333
## 9     2  484            69  40.333333
## 10    2  664           111  55.333333
## 11    2 1004           156  83.666667
## 12    2 1231           172 102.583333
## 13    2 1372           203 114.333333
## 14    2 1582           203 131.833333
## 15    3  118            30   9.833333
## 16    3  484            51  40.333333
## 17    3  664            75  55.333333
## 18    3 1004           108  83.666667
## 19    3 1231           115 102.583333
## 20    3 1372           139 114.333333
## 21    3 1582           140 131.833333
## 22    4  118            32   9.833333
## 23    4  484            62  40.333333
## 24    4  664           112  55.333333
## 25    4 1004           167  83.666667
## 26    4 1231           179 102.583333
## 27    4 1372           209 114.333333
## 28    4 1582           214 131.833333
## 29    5  118            30   9.833333
## 30    5  484            49  40.333333
## 31    5  664            81  55.333333
## 32    5 1004           125  83.666667
## 33    5 1231           142 102.583333
## 34    5 1372           174 114.333333
## 35    5 1582           177 131.833333
rubah = data %>%
  mutate(age/12)
rubah
##    Tree  age circumference     age/12
## 1     1  118            30   9.833333
## 2     1  484            58  40.333333
## 3     1  664            87  55.333333
## 4     1 1004           115  83.666667
## 5     1 1231           120 102.583333
## 6     1 1372           142 114.333333
## 7     1 1582           145 131.833333
## 8     2  118            33   9.833333
## 9     2  484            69  40.333333
## 10    2  664           111  55.333333
## 11    2 1004           156  83.666667
## 12    2 1231           172 102.583333
## 13    2 1372           203 114.333333
## 14    2 1582           203 131.833333
## 15    3  118            30   9.833333
## 16    3  484            51  40.333333
## 17    3  664            75  55.333333
## 18    3 1004           108  83.666667
## 19    3 1231           115 102.583333
## 20    3 1372           139 114.333333
## 21    3 1582           140 131.833333
## 22    4  118            32   9.833333
## 23    4  484            62  40.333333
## 24    4  664           112  55.333333
## 25    4 1004           167  83.666667
## 26    4 1231           179 102.583333
## 27    4 1372           209 114.333333
## 28    4 1582           214 131.833333
## 29    5  118            30   9.833333
## 30    5  484            49  40.333333
## 31    5  664            81  55.333333
## 32    5 1004           125  83.666667
## 33    5 1231           142 102.583333
## 34    5 1372           174 114.333333
## 35    5 1582           177 131.833333
kolom_baru = data %>%
  mutate(age2 = age/12)
View(kolom_baru)
head(kolom_baru)
##   Tree  age circumference       age2
## 1    1  118            30   9.833333
## 2    1  484            58  40.333333
## 3    1  664            87  55.333333
## 4    1 1004           115  83.666667
## 5    1 1231           120 102.583333
## 6    1 1372           142 114.333333

Select data

select_data = select(data, Tree,age)
select_data
##    Tree  age
## 1     1  118
## 2     1  484
## 3     1  664
## 4     1 1004
## 5     1 1231
## 6     1 1372
## 7     1 1582
## 8     2  118
## 9     2  484
## 10    2  664
## 11    2 1004
## 12    2 1231
## 13    2 1372
## 14    2 1582
## 15    3  118
## 16    3  484
## 17    3  664
## 18    3 1004
## 19    3 1231
## 20    3 1372
## 21    3 1582
## 22    4  118
## 23    4  484
## 24    4  664
## 25    4 1004
## 26    4 1231
## 27    4 1372
## 28    4 1582
## 29    5  118
## 30    5  484
## 31    5  664
## 32    5 1004
## 33    5 1231
## 34    5 1372
## 35    5 1582
pilih = data %>%
  select(Tree, age)
pilih
##    Tree  age
## 1     1  118
## 2     1  484
## 3     1  664
## 4     1 1004
## 5     1 1231
## 6     1 1372
## 7     1 1582
## 8     2  118
## 9     2  484
## 10    2  664
## 11    2 1004
## 12    2 1231
## 13    2 1372
## 14    2 1582
## 15    3  118
## 16    3  484
## 17    3  664
## 18    3 1004
## 19    3 1231
## 20    3 1372
## 21    3 1582
## 22    4  118
## 23    4  484
## 24    4  664
## 25    4 1004
## 26    4 1231
## 27    4 1372
## 28    4 1582
## 29    5  118
## 30    5  484
## 31    5  664
## 32    5 1004
## 33    5 1231
## 34    5 1372
## 35    5 1582

Kimbinasi Fungsi

jumlah_pohon1 = data %>%
  filter(Tree == '1') %>%
    summarise (sum(circumference))
jumlah_pohon1
##   sum(circumference)
## 1                697
jumlah_pohon2 = data %>% 
  filter(Tree == '2') %>%
    summarise (sum(circumference))
jumlah_pohon2
##   sum(circumference)
## 1                947
susun_ulang = data %>%
  select(Tree, age, circumference) %>%
    filter(Tree == '1') %>%
      arrange(desc(age))%>%
        mutate(circumference2= circumference/2)
susun_ulang
##   Tree  age circumference circumference2
## 1    1 1582           145           72.5
## 2    1 1372           142           71.0
## 3    1 1231           120           60.0
## 4    1 1004           115           57.5
## 5    1  664            87           43.5
## 6    1  484            58           29.0
## 7    1  118            30           15.0
jumlah_susun_ulang = data %>%
  select(Tree, age, circumference) %>%
    filter(Tree == '1') %>%
      arrange(desc(age))%>%
        mutate(circumference2= circumference/2) %>%
          summarise(sum(circumference2))
jumlah_susun_ulang
##   sum(circumference2)
## 1               348.5

#test hasil dengan excel

write.csv(jumlah_pohon2, "dataoke.csv")