Sei sulla pagina 1di 13

R version 3.5.

2 (2018-12-20) -- "Eggshell Igloo"


Copyright (C) 2018 The R Foundation for Statistical Computing
Platform: i386-w64-mingw32/i386 (32-bit)

R is free software and comes with ABSOLUTELY NO WARRANTY.


You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.
R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or


'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.

> install.packages("ggplot2")
trying URL 'https://cran.rstudio.com/bin/windows/contrib/3.5/ggplot2_3.1.1.zip'
Content type 'application/zip' length 3621814 bytes (3.5 MB)
downloaded 3.5 MB

package ‘ggplot2’ successfully unpacked and MD5 sums checked


The downloaded binary packages are in
C:\Users\Students\AppData\Local\Temp\RtmpGMkArA\downloaded_packages
>
>
> install.packages("ggalt")
also installing the dependencies ‘httpuv’, ‘xtable’, ‘sourcetools’, ‘shiny’, ‘later’, ‘
tidyselect’, ‘BH’, ‘plogr’, ‘extrafontdb’, ‘Rttf2pt1’, ‘base64enc’, ‘htmltools’, ‘htmlw
idgets’, ‘tidyr’, ‘hexbin’, ‘crosstalk’, ‘purrr’, ‘data.table’, ‘promises’, ‘dplyr’, ‘p
roj4’, ‘ash’, ‘maps’, ‘extrafont’, ‘plotly’

trying URL 'https://cran.rstudio.com/bin/windows/contrib/3.5/httpuv_1.5.1.zip'


Content type 'application/zip' length 1492078 bytes (1.4 MB)
downloaded 1.4 MB

trying URL 'https://cran.rstudio.com/bin/windows/contrib/3.5/xtable_1.8-4.zip'


Content type 'application/zip' length 704814 bytes (688 KB)
downloaded 688 KB

trying URL 'https://cran.rstudio.com/bin/windows/contrib/3.5/sourcetools_0.1.7.zip'


Content type 'application/zip' length 530455 bytes (518 KB)
downloaded 518 KB

trying URL 'https://cran.rstudio.com/bin/windows/contrib/3.5/shiny_1.3.2.zip'


Content type 'application/zip' length 4672321 bytes (4.5 MB)
downloaded 4.5 MB

trying URL 'https://cran.rstudio.com/bin/windows/contrib/3.5/later_0.8.0.zip'


Content type 'application/zip' length 610980 bytes (596 KB)
downloaded 596 KB

trying URL 'https://cran.rstudio.com/bin/windows/contrib/3.5/tidyselect_0.2.5.zip'


Content type 'application/zip' length 625697 bytes (611 KB)
downloaded 611 KB

trying URL 'https://cran.rstudio.com/bin/windows/contrib/3.5/BH_1.69.0-1.zip'


Content type 'application/zip' length 19008161 bytes (18.1 MB)
downloaded 15.7 MB

Warning in install.packages :
downloaded length 16478208 != reported length 19008161
trying URL 'https://cran.rstudio.com/bin/windows/contrib/3.5/plogr_0.2.0.zip'
Content type 'application/zip' length 18731 bytes (18 KB)
downloaded 18 KB
trying URL 'https://cran.rstudio.com/bin/windows/contrib/3.5/extrafontdb_1.0.zip'
Content type 'application/zip' length 10261 bytes (10 KB)
downloaded 10 KB
trying URL 'https://cran.rstudio.com/bin/windows/contrib/3.5/Rttf2pt1_1.3.7.zip'
Content type 'application/zip' length 117470 bytes (114 KB)
downloaded 114 KB

trying URL 'https://cran.rstudio.com/bin/windows/contrib/3.5/base64enc_0.1-3.zip'


Content type 'application/zip' length 43316 bytes (42 KB)
downloaded 42 KB

trying URL 'https://cran.rstudio.com/bin/windows/contrib/3.5/htmltools_0.3.6.zip'


Content type 'application/zip' length 660862 bytes (645 KB)
downloaded 645 KB
trying URL 'https://cran.rstudio.com/bin/windows/contrib/3.5/htmlwidgets_1.3.zip'
Content type 'application/zip' length 805089 bytes (786 KB)
downloaded 786 KB

trying URL 'https://cran.rstudio.com/bin/windows/contrib/3.5/tidyr_0.8.3.zip'


Content type 'application/zip' length 953683 bytes (931 KB)
downloaded 931 KB

trying URL 'https://cran.rstudio.com/bin/windows/contrib/3.5/hexbin_1.27.2.zip'


Content type 'application/zip' length 836401 bytes (816 KB)
downloaded 816 KB
trying URL 'https://cran.rstudio.com/bin/windows/contrib/3.5/crosstalk_1.0.0.zip'
Content type 'application/zip' length 665341 bytes (649 KB)
downloaded 649 KB

trying URL 'https://cran.rstudio.com/bin/windows/contrib/3.5/purrr_0.3.2.zip'


Content type 'application/zip' length 417615 bytes (407 KB)
downloaded 407 KB

trying URL 'https://cran.rstudio.com/bin/windows/contrib/3.5/data.table_1.12.2.zip'


Content type 'application/zip' length 1938681 bytes (1.8 MB)
downloaded 1.8 MB

trying URL 'https://cran.rstudio.com/bin/windows/contrib/3.5/promises_1.0.1.zip'


Content type 'application/zip' length 695169 bytes (678 KB)
downloaded 678 KB

trying URL 'https://cran.rstudio.com/bin/windows/contrib/3.5/dplyr_0.8.0.1.zip'


Content type 'application/zip' length 3251670 bytes (3.1 MB)
downloaded 3.1 MB

trying URL 'https://cran.rstudio.com/bin/windows/contrib/3.5/proj4_1.0-8.zip'


Content type 'application/zip' length 316227 bytes (308 KB)
downloaded 308 KB
trying URL 'https://cran.rstudio.com/bin/windows/contrib/3.5/ash_1.0-15.zip'
Content type 'application/zip' length 41094 bytes (40 KB)
downloaded 40 KB

trying URL 'https://cran.rstudio.com/bin/windows/contrib/3.5/maps_3.3.0.zip'


Content type 'application/zip' length 3694192 bytes (3.5 MB)
downloaded 3.5 MB

trying URL 'https://cran.rstudio.com/bin/windows/contrib/3.5/extrafont_0.17.zip'


Content type 'application/zip' length 51510 bytes (50 KB)
downloaded 50 KB
trying URL 'https://cran.rstudio.com/bin/windows/contrib/3.5/plotly_4.9.0.zip'
Content type 'application/zip' length 2841567 bytes (2.7 MB)
downloaded 2.7 MB

trying URL 'https://cran.rstudio.com/bin/windows/contrib/3.5/ggalt_0.4.0.zip'


Content type 'application/zip' length 2350646 bytes (2.2 MB)
downloaded 2.2 MB

package ‘httpuv’ successfully unpacked and MD5 sums checked


package ‘xtable’ successfully unpacked and MD5 sums checked
package ‘sourcetools’ successfully unpacked and MD5 sums checked
package ‘shiny’ successfully unpacked and MD5 sums checked
package ‘later’ successfully unpacked and MD5 sums checked
package ‘tidyselect’ successfully unpacked and MD5 sums checked
Warning in install.packages :
error 1 in extracting from zip file
Warning in install.packages :
cannot open compressed file 'BH/DESCRIPTION', probable reason 'No such file or direct
ory'
Error in install.packages : cannot open the connection
>
> midwest
Error: object 'midwest' not found
> iris
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1 5.1 3.5 1.4 0.2 setosa
2 4.9 3.0 1.4 0.2 setosa
3 4.7 3.2 1.3 0.2 setosa
4 4.6 3.1 1.5 0.2 setosa
5 5.0 3.6 1.4 0.2 setosa
6 5.4 3.9 1.7 0.4 setosa
7 4.6 3.4 1.4 0.3 setosa
8 5.0 3.4 1.5 0.2 setosa
9 4.4 2.9 1.4 0.2 setosa
10 4.9 3.1 1.5 0.1 setosa
11 5.4 3.7 1.5 0.2 setosa
12 4.8 3.4 1.6 0.2 setosa
13 4.8 3.0 1.4 0.1 setosa
14 4.3 3.0 1.1 0.1 setosa
15 5.8 4.0 1.2 0.2 setosa
16 5.7 4.4 1.5 0.4 setosa
17 5.4 3.9 1.3 0.4 setosa
18 5.1 3.5 1.4 0.3 setosa
19 5.7 3.8 1.7 0.3 setosa
20 5.1 3.8 1.5 0.3 setosa
21 5.4 3.4 1.7 0.2 setosa
22 5.1 3.7 1.5 0.4 setosa
23 4.6 3.6 1.0 0.2 setosa
24 5.1 3.3 1.7 0.5 setosa
25 4.8 3.4 1.9 0.2 setosa
26 5.0 3.0 1.6 0.2 setosa
27 5.0 3.4 1.6 0.4 setosa
28 5.2 3.5 1.5 0.2 setosa
29 5.2 3.4 1.4 0.2 setosa
30 4.7 3.2 1.6 0.2 setosa
31 4.8 3.1 1.6 0.2 setosa
32 5.4 3.4 1.5 0.4 setosa
33 5.2 4.1 1.5 0.1 setosa
34 5.5 4.2 1.4 0.2 setosa
35 4.9 3.1 1.5 0.2 setosa
36 5.0 3.2 1.2 0.2 setosa
37 5.5 3.5 1.3 0.2 setosa
38 4.9 3.6 1.4 0.1 setosa
39 4.4 3.0 1.3 0.2 setosa
40 5.1 3.4 1.5 0.2 setosa
41 5.0 3.5 1.3 0.3 setosa
42 4.5 2.3 1.3 0.3 setosa
43 4.4 3.2 1.3 0.2 setosa
44 5.0 3.5 1.6 0.6 setosa
45 5.1 3.8 1.9 0.4 setosa
46 4.8 3.0 1.4 0.3 setosa
47 5.1 3.8 1.6 0.2 setosa
48 4.6 3.2 1.4 0.2 setosa
49 5.3 3.7 1.5 0.2 setosa
50 5.0 3.3 1.4 0.2 setosa
51 7.0 3.2 4.7 1.4 versicolor
52 6.4 3.2 4.5 1.5 versicolor
53 6.9 3.1 4.9 1.5 versicolor
54 5.5 2.3 4.0 1.3 versicolor
55 6.5 2.8 4.6 1.5 versicolor
56 5.7 2.8 4.5 1.3 versicolor
57 6.3 3.3 4.7 1.6 versicolor
58 4.9 2.4 3.3 1.0 versicolor
59 6.6 2.9 4.6 1.3 versicolor
60 5.2 2.7 3.9 1.4 versicolor
61 5.0 2.0 3.5 1.0 versicolor
62 5.9 3.0 4.2 1.5 versicolor
63 6.0 2.2 4.0 1.0 versicolor
64 6.1 2.9 4.7 1.4 versicolor
65 5.6 2.9 3.6 1.3 versicolor
66 6.7 3.1 4.4 1.4 versicolor
67 5.6 3.0 4.5 1.5 versicolor
68 5.8 2.7 4.1 1.0 versicolor
69 6.2 2.2 4.5 1.5 versicolor
70 5.6 2.5 3.9 1.1 versicolor
71 5.9 3.2 4.8 1.8 versicolor
72 6.1 2.8 4.0 1.3 versicolor
73 6.3 2.5 4.9 1.5 versicolor
74 6.1 2.8 4.7 1.2 versicolor
75 6.4 2.9 4.3 1.3 versicolor
76 6.6 3.0 4.4 1.4 versicolor
77 6.8 2.8 4.8 1.4 versicolor
78 6.7 3.0 5.0 1.7 versicolor
79 6.0 2.9 4.5 1.5 versicolor
80 5.7 2.6 3.5 1.0 versicolor
81 5.5 2.4 3.8 1.1 versicolor
82 5.5 2.4 3.7 1.0 versicolor
83 5.8 2.7 3.9 1.2 versicolor
84 6.0 2.7 5.1 1.6 versicolor
85 5.4 3.0 4.5 1.5 versicolor
86 6.0 3.4 4.5 1.6 versicolor
87 6.7 3.1 4.7 1.5 versicolor
88 6.3 2.3 4.4 1.3 versicolor
89 5.6 3.0 4.1 1.3 versicolor
90 5.5 2.5 4.0 1.3 versicolor
91 5.5 2.6 4.4 1.2 versicolor
92 6.1 3.0 4.6 1.4 versicolor
93 5.8 2.6 4.0 1.2 versicolor
94 5.0 2.3 3.3 1.0 versicolor
95 5.6 2.7 4.2 1.3 versicolor
96 5.7 3.0 4.2 1.2 versicolor
97 5.7 2.9 4.2 1.3 versicolor
98 6.2 2.9 4.3 1.3 versicolor
99 5.1 2.5 3.0 1.1 versicolor
100 5.7 2.8 4.1 1.3 versicolor
101 6.3 3.3 6.0 2.5 virginica
102 5.8 2.7 5.1 1.9 virginica
103 7.1 3.0 5.9 2.1 virginica
104 6.3 2.9 5.6 1.8 virginica
105 6.5 3.0 5.8 2.2 virginica
106 7.6 3.0 6.6 2.1 virginica
107 4.9 2.5 4.5 1.7 virginica
108 7.3 2.9 6.3 1.8 virginica
109 6.7 2.5 5.8 1.8 virginica
110 7.2 3.6 6.1 2.5 virginica
111 6.5 3.2 5.1 2.0 virginica
112 6.4 2.7 5.3 1.9 virginica
113 6.8 3.0 5.5 2.1 virginica
114 5.7 2.5 5.0 2.0 virginica
115 5.8 2.8 5.1 2.4 virginica
116 6.4 3.2 5.3 2.3 virginica
117 6.5 3.0 5.5 1.8 virginica
118 7.7 3.8 6.7 2.2 virginica
119 7.7 2.6 6.9 2.3 virginica
120 6.0 2.2 5.0 1.5 virginica
121 6.9 3.2 5.7 2.3 virginica
122 5.6 2.8 4.9 2.0 virginica
123 7.7 2.8 6.7 2.0 virginica
124 6.3 2.7 4.9 1.8 virginica
125 6.7 3.3 5.7 2.1 virginica
126 7.2 3.2 6.0 1.8 virginica
127 6.2 2.8 4.8 1.8 virginica
128 6.1 3.0 4.9 1.8 virginica
129 6.4 2.8 5.6 2.1 virginica
130 7.2 3.0 5.8 1.6 virginica
131 7.4 2.8 6.1 1.9 virginica
132 7.9 3.8 6.4 2.0 virginica
133 6.4 2.8 5.6 2.2 virginica
134 6.3 2.8 5.1 1.5 virginica
135 6.1 2.6 5.6 1.4 virginica
136 7.7 3.0 6.1 2.3 virginica
137 6.3 3.4 5.6 2.4 virginica
138 6.4 3.1 5.5 1.8 virginica
139 6.0 3.0 4.8 1.8 virginica
140 6.9 3.1 5.4 2.1 virginica
141 6.7 3.1 5.6 2.4 virginica
142 6.9 3.1 5.1 2.3 virginica
143 5.8 2.7 5.1 1.9 virginica
144 6.8 3.2 5.9 2.3 virginica
145 6.7 3.3 5.7 2.5 virginica
146 6.7 3.0 5.2 2.3 virginica
147 6.3 2.5 5.0 1.9 virginica
148 6.5 3.0 5.2 2.0 virginica
149 6.2 3.4 5.4 2.3 virginica
150 5.9 3.0 5.1 1.8 virginica
> ggplot(midwest,aes(x=sepal.length, y=sepal.width))
Error in ggplot(midwest, aes(x = sepal.length, y = sepal.width)) :
could not find function "ggplot"
> ggplot(midwest,aes(x=Sepal.length, y=Sepal.width))
Error in ggplot(midwest, aes(x = Sepal.length, y = Sepal.width)) :
could not find function "ggplot"
> str(iris)
'data.frame': 150 obs. of 5 variables:
$ Sepal.Length: num 5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
$ Sepal.Width : num 3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
$ Petal.Length: num 1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
$ Petal.Width : num 0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
$ Species : Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1 1 1 1 1 ...
> ggplot(iris, aes(x=Sepal.length, y=Sepal.width))
Error in ggplot(iris, aes(x = Sepal.length, y = Sepal.width)) :
could not find function "ggplot"
> ggplot(iris,aes(x=Sepal.length, y=Sepal.width))
Error in ggplot(iris, aes(x = Sepal.length, y = Sepal.width)) :
could not find function "ggplot"
> library(ggplot2)
Warning message:
package ‘ggplot2’ was built under R version 3.5.3
>
> ggplot(iris,aes(x=Sepal.length, y=Sepal.width))
Error in FUN(X[[i]], ...) : object 'Sepal.length' not found
> ggplot(iris,aes(x=Sepal.length,y=Sepal.width))
Error in FUN(X[[i]], ...) : object 'Sepal.length' not found
> library(ggplot2)
>
> library(ggplot2)
>
>
> library(ggplot2)
>
> ggplot(iris,aes(x=Sepal.length,y=Sepal.width))
Error in FUN(X[[i]], ...) : object 'Sepal.length' not found
>
> ggplot(iris,aes(x=Sepal.length,y=Sepal.width))
Error in FUN(X[[i]], ...) : object 'Sepal.length' not found
>
>
>
>
>
>
> install.packages("ggplot2")
Error in install.packages : Updating loaded packages
> install.packages("ggplot2")
Warning in install.packages :
package ‘ggplot2’ is in use and will not be installed
> library(ggplot2)
>
> geom_point(aes(col=state,size=popdensity))
mapping: colour = ~state, size = ~popdensity
geom_point: na.rm = FALSE
stat_identity: na.rm = FALSE
position_identity
> ggplot(iris,aes(x=Sepal.Length,y=Sepal.width)) +geom_point(aes(col=Species,size=Petal
.length))
Error in FUN(X[[i]], ...) : object 'Petal.length' not found
> +geom_point(aes(col=Species,size=Petal.length))
Error: Cannot use `+.gg()` with a single argument. Did you accidentally put + on a new
line?
> geom_point(aes(col=Species,size=Petal.length))
mapping: colour = ~Species, size = ~Petal.length
geom_point: na.rm = FALSE
stat_identity: na.rm = FALSE
position_identity
>
> ggplot(iris,aes(x=Sepal.Length,y=Sepal.width))+geom_point(aes(col=Species,size=Petal.
length))
Error in FUN(X[[i]], ...) : object 'Petal.length' not found
> ggplot(iris,aes(x=Sepal.Length,y=Sepal.width))+geom_point(aes(col=Species,size=Petal.
length))
Error in FUN(X[[i]], ...) : object 'Petal.length' not found
> ggplot(iris,aes(x=Sepal.Length,y=Sepal.Width))+geom_point(aes(col=Species,size=Petal.
Length))
> ggplot(iris,aes(x=Sepal.Length,y=Sepal.Width))+geom_point(aes(col=Species,size=Petal.
Length))
> ggplot(iris,aes(x=Sepal.Length,y=Sepal.Width))+geom_point(aes(col=Species,size=Petal.
Length))+geom_smooth(method="loess,se=F")
Warning message:
Computation failed in `stat_smooth()`:
object 'loess,se=F' of mode 'function' was not found
> ggplot(iris,aes(x=Sepal.Length,y=Sepal.Width))+geom_point(aes(col=Species,size=Petal.
Length))+geom_smooth(method="loess,se=F")
Warning message:
Computation failed in `stat_smooth()`:
object 'loess,se=F' of mode 'function' was not found
> ggplot(iris,aes(x=Sepal.Length,y=Sepal.Width))+geom_point(aes(col=Species,size=Petal.
Length))+geom_smooth(method="loess,se=P")
Warning message:
Computation failed in `stat_smooth()`:
object 'loess,se=P' of mode 'function' was not found
> ggplot(iris,aes(x=Sepal.Length,y=Sepal.Width))+geom_point(aes(col=Species,size=Petal.
Length))+geom_smooth(method="loess,se=P")
Warning message:
Computation failed in `stat_smooth()`:
object 'loess,se=P' of mode 'function' was not found
>
>
>
>
>
>
>
>
> ggplot(iris,aes(x=Sepal.Length,y=Sepal.Width))+geom_point(aes(col=Species,size=Petal.
Length))+geom_smooth(method="loess,se=P")+xlim(c(0,0.1))
Warning messages:
1: Removed 150 rows containing non-finite values (stat_smooth).
2: Removed 150 rows containing missing values (geom_point).
> ggplot(iris,aes(x=Sepal.Length,y=Sepal.Width))+geom_point(aes(col=Species,size=Petal.
Length))+geom_smooth(method="loess,se=P")+xlim(c(0,0.1))+ylim(c(0,500000))
Warning messages:
1: Removed 150 rows containing non-finite values (stat_smooth).
2: Removed 150 rows containing missing values (geom_point).
> summary(iris)
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
Min. :4.300 Min. :2.000 Min. :1.000 Min. :0.100 setosa :50
1st Qu.:5.100 1st Qu.:2.800 1st Qu.:1.600 1st Qu.:0.300 versicolor:50
Median :5.800 Median :3.000 Median :4.350 Median :1.300 virginica :50
Mean :5.843 Mean :3.057 Mean :3.758 Mean :1.199
3rd Qu.:6.400 3rd Qu.:3.300 3rd Qu.:5.100 3rd Qu.:1.800
Max. :7.900 Max. :4.400 Max. :6.900 Max. :2.500

Selection: 13

| | 0%

| One of the great advantages of using a statistical programming language like R is


| its vast collection of tools for simulating random numbers.

...

|== | 3%
| This lesson assumes familiarity with a few common probability distributions, but
| these topics will only be discussed with respect to random number generation. Even
| if you have no prior experience with these concepts, you should be able to
| complete the lesson and understand the main ideas.

...

|===== | 6%
| The first function we'll use to generate random numbers is sample(). Use ?sample
| to pull up the documentation.

> ?sample

| Excellent job!
|======= | 9%
| Let's simulate rolling four six-sided dice: sample(1:6, 4, replace = TRUE).
> sample(1:6, 4, replace = TRUE)
[1] 6 3 3 3

| You are amazing!

|========= | 12%
| Now repeat the command to see how your result differs. (The probability of rolling
| the exact same result is (1/6)^4 = 0.00077, which is pretty small!)

> sample(1:6^4 = 0.00077)


Error: unexpected '=' in "sample(1:6^4 ="
> sample((1/6)^4 = 0.00077)
Error: unexpected '=' in "sample((1/6)^4 ="
>
> sample(1:6, 4, replace = TRUE)
[1] 1 2 6 5

| Perseverance, that's the answer.


|=========== | 15%
| sample(1:6, 4, replace = TRUE) instructs R to randomly select four numbers between
| 1 and 6, WITH replacement. Sampling with replacement simply means that each number
| is "replaced" after it is selected, so that the same number can show up more than
| once. This is what we want here, since what you roll on one die shouldn't affect
| what you roll on any of the others.

...
|============== | 18%
| Now sample 10 numbers between 1 and 20, WITHOUT replacement. To sample without
| replacement, simply leave off the 'replace' argument.

> sample(2, 4, 6, 7, 8, 20, 12, 14, 16, 18, 20)


Error in sample(2, 4, 6, 7, 8, 20, 12, 14, 16, 18, 20) :
unused arguments (8, 20, 12, 14, 16, 18, 20)
> sample(2, 4, 6, 7, 8, 20, 12, 14, 16, 18, 20, replace)
Error in sample(2, 4, 6, 7, 8, 20, 12, 14, 16, 18, 20, replace) :
unused arguments (8, 20, 12, 14, 16, 18, 20, replace)
> sample(2, 4, 6, 7, 8, 20, 12, 14, 16, 18, 20, replace = TRUE)
Error in sample(2, 4, 6, 7, 8, 20, 12, 14, 16, 18, 20, replace = TRUE) :
unused arguments (7, 8, 20, 12, 14, 16, 18, 20)
> sample(1:20, 2, 4, 6, 7, 8, 20, 12, 14, 16, 18, 20, replace = TRUE)
Error in sample(1:20, 2, 4, 6, 7, 8, 20, 12, 14, 16, 18, 20, replace = TRUE) :
unused arguments (6, 7, 8, 20, 12, 14, 16, 18, 20)
> sample(1:20, 2, 4, 6, 7, 8, 20, 12, 14, 16, 18, 20)
Error in sample(1:20, 2, 4, 6, 7, 8, 20, 12, 14, 16, 18, 20) :
unused arguments (7, 8, 20, 12, 14, 16, 18, 20)
> rplace
Error: object 'rplace' not found
> replace(1, 2, 3 ,4 ,5 , 6, 7, 8, 9, 10)
Error in replace(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) :
unused arguments (4, 5, 6, 7, 8, 9, 10)
> sample(1:20, 10)
[1] 15 12 3 18 19 5 17 8 11 4
| That's the answer I was looking for.

|================ | 21%
| Since the last command sampled without replacement, no number appears more than
| once in the output.

...

|================== | 24%
| LETTERS is a predefined variable in R containing a vector of all 26 letters of the
| English alphabet. Take a look at it now.
> R <-c(alpha(26))

| Not quite right, but keep trying. Or, type info() for more options.

| Type LETTERS to print its contents to the console.


> R <-c(alpha(A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y
, Z)
+
+ H
Error: unexpected symbol in:
"
H"
> R <-c(alpha(A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y
, Z)
+
+ 26
Error: unexpected numeric constant in:
"
26"
> A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z)
Error: unexpected ',' in "A,"
> A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z
Error: unexpected ',' in "A,"
> R(A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z)
Error in R(A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, :
could not find function "R"
> alpha(A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z)
Error in alpha(A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, :
unused arguments (C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y
, Z)
> LETTERS
[1] "A" "B" "C" "D" "E" "F" "G" "H" "I" "J" "K" "L" "M" "N" "O" "P" "Q" "R" "S" "T"
[21] "U" "V" "W" "X" "Y" "Z"

| Nice work!

|==================== | 27%
| The sample() function can also be used to permute, or rearrange, the elements of a
| vector. For example, try sample(LETTERS) to permute all 26 letters of the English
| alphabet.
> sample(LETTERS)
[1] "R" "C" "Q" "P" "T" "X" "O" "K" "F" "G" "U" "A" "H" "I" "D" "E" "L" "J" "Y" "W"
[21] "Z" "M" "V" "N" "S" "B"

| Your dedication is inspiring!

|======================= | 30%
| This is identical to taking a sample of size 26 from LETTERS, without replacement.
| When the 'size' argument to sample() is not specified, R takes a sample equal in
| size to the vector from which you are sampling.

...

|========================= | 33%
| Now, suppose we want to simulate 100 flips of an unfair two-sided coin. This
| particular coin has a 0.3 probability of landing 'tails' and a 0.7 probability of
| landing 'heads'.
...

|=========================== | 36%
| Let the value 0 represent tails and the value 1 represent heads. Use sample() to
| draw a sample of size 100 from the vector c(0,1), with replacement. Since the coin
| is unfair, we must attach specific probabilities to the values 0 (tails) and 1
| (heads) with a fourth argument, prob = c(0.3, 0.7). Assign the result to a new
| variable called flips.
> sample(1:100, replace c(0, 1))
Error: unexpected symbol in "sample(1:100, replace c"
> sample(1:100)
[1] 52 28 34 76 26 24 82 58 69 11 2 99 55 98 96 78 27 68 40 44
[21] 49 75 29 83 61 47 43 74 60 25 6 91 7 59 66 100 12 63 5 90
[41] 85 94 80 13 92 56 39 30 64 71 57 16 1 8 62 23 54 37 20 86
[61] 32 87 18 33 50 77 67 22 41 84 17 35 65 79 4 42 73 72 14 97
[81] 48 21 89 51 38 88 15 10 46 93 81 95 36 19 31 45 70 53 9 3
| Almost! Try again. Or, type info() for more options.

| The following command will produce 100 flips of an unfair coin and assign the
| result: flips <- sample(c(0,1), 100, replace = TRUE, prob = c(0.3, 0.7))

> flips <- sample(c(0,1), 100, replace = TRUE, prob = c(0.3, 0.7))

| You got it right!


|============================== | 39%
| View the contents of the flips variable.
>
> flips
[1] 1 1 1 1 1 1 1 1 1 1 0 1 1 0 1 1 0 0 1 1 0 1 1 0 1 1 1 1 1 0 1 1 1 0 1 0 0 1 1 0
[41] 1 1 0 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 0 0 0 1 0 0 1 0 1 1 1 0 1 1 0 1 1
[81] 0 1 0 1 1 1 0 0 0 1 1 1 0 0 1 1 0 1 0 1

| You got it right!

|================================ | 42%
| Since we set the probability of landing heads on any given flip to be 0.7, we'd
| expect approximately 70 of our coin flips to have the value 1. Count the actual
| number of 1s contained in flips using the sum() function.

> sum(1)
[1] 1

| You almost had it, but not quite. Try again. Or, type info() for more options.
| sum(flips) will add up all the 1s and 0s, giving you the total number of 1s in
| flips.
> sum(flips)
[1] 69

| You are quite good my friend!

|================================== | 45%
| A coin flip is a binary outcome (0 or 1) and we are performing 100 independent
| trials (coin flips), so we can use rbinom() to simulate a binomial random
| variable. Pull up the documentation for rbinom() using ?rbinom.

> ?rbinom
| You got it!

|==================================== | 48%
| Each probability distribution in R has an r*** function (for "random"), a d***
| function (for "density"), a p*** (for "probability"), and q*** (for "quantile").
| We are most interested in the r*** functions in this lesson, but I encourage you
| to explore the others on your own.

...

|======================================= | 52%
| A binomial random variable represents the number of 'successes' (heads) in a given
| number of independent 'trials' (coin flips). Therefore, we can generate a single
| random variable that represents the number of heads in 100 flips of our unfair
| coin using rbinom(1, size = 100, prob = 0.7). Note that you only specify the
| probability of 'success' (heads) and NOT the probability of 'failure' (tails). Try
| it now.
> rbinom(1, size = 100, prob = 0.7)
[1] 71
| Keep working like that and you'll get there!
|========================================= | 55%
| Equivalently, if we want to see all of the 0s and 1s, we can request 100
| observations, each of size 1, with success probability of 0.7. Give it a try,
| assigning the result to a new variable called flips2.

> flips2 <-sample(c(0, 1), 100, size = 1, prob = 0.7)


Error in sample.int(length(x), size, replace, prob) :
incorrect number of probabilities
> flips2 <-sample(c(0, 1), 100, replace = TRUE, size = 1, prob = 0.7)
Error in sample(c(0, 1), 100, replace = TRUE, size = 1, prob = 0.7) :
unused argument (100)
> info()
| When you are at the R prompt (>):
| -- Typing skip() allows you to skip the current question.
| -- Typing play() lets you experiment with R on your own; swirl will ignore what
| you do...
| -- UNTIL you type nxt() which will regain swirl's attention.
| -- Typing bye() causes swirl to exit. Your progress will be saved.
| -- Typing main() returns you to swirl's main menu.
| -- Typing info() displays these options again.
> flips2 <-100(c(0, 1), replace = TRUE, size = 1, prob = 0.7)
Error: attempt to apply non-function
> nxt
function ()
{
invisible()
}
<bytecode: 0x15444018>
<environment: namespace:swirl>

| You almost had it, but not quite. Try again. Or, type info() for more options.
| Call rbinom() with n = 100, size = 1, and prob = 0.7 and assign the result to
| flips2.

> rbinom(100, size = 1, prob = 0.7)


[1] 1 0 0 1 0 1 1 1 1 1 1 0 1 0 1 1 0 1 1 1 1 1 0 1 1 0 0 1 0 0 0 0 1 1 0 0 1 0 1 0
[41] 1 0 1 1 1 0 1 1 1 1 1 1 0 1 1 0 1 1 1 0 1 1 1 1 1 1 1 1 0 1 1 1 0 1 1 0 1 1 0 1
[81] 0 1 1 0 1 1 1 1 1 0 0 1 0 1 1 0 1 0 0 1

| Almost! Try again. Or, type info() for more options.

| Call rbinom() with n = 100, size = 1, and prob = 0.7 and assign the result to
| flips2.

> rbinom(100, size = 1, prob = 0.7)flips2


Error: unexpected symbol in "rbinom(100, size = 1, prob = 0.7)flips2"
> flips2 <-rbinom(100, size = 1, prob = 0.7)

| That's the answer I was looking for.

|=========================================== | 58%
| View the contents of flips2.

> flips2
[1] 1 1 1 0 0 0 1 1 0 0 1 1 0 0 1 1 1 0 1 1 0 1 1 1 1 1 1 1 1 1 0 1 0 0 0 1 1 1 1 1
[41] 1 1 0 1 1 1 1 1 1 1 0 1 0 0 0 1 0 1 1 1 0 1 0 1 0 0 0 1 0 0 1 1 1 1 1 1 1 0 1 1
[81] 1 1 1 1 1 0 1 1 1 1 1 1 0 0 0 1 1 1 1 0
| You got it!

|============================================= | 61%
| Now use sum() to count the number of 1s (heads) in flips2. It should be close to
| 70!
> sum(heads)
Error: object 'heads' not found
> sum()
[1] 0

| Nice try, but that's not exactly what I was hoping for. Try again. Or, type info()
| for more options.

| Use sum(flips2) to count the number of 1s.

> sum(flips2)
[1] 68

| Excellent job!
|================================================ | 64%
| Similar to rbinom(), we can use R to simulate random numbers from many other
| probability distributions. Pull up the documentation for rnorm() now.

> rbinom(flips2)
Error in rbinom(flips2) : argument "size" is missing, with no default
> rnorm
function (n, mean = 0, sd = 1)
.Call(C_rnorm, n, mean, sd)
<bytecode: 0x10b4cb58>
<environment: namespace:stats>
| That's not exactly what I'm looking for. Try again. Or, type info() for more
| options.
| Type ?rnorm to view its help file.

> ?rnorm

| That's a job well done!


|================================================== | 67%
| The standard normal distribution has mean 0 and standard deviation 1. As you can
| see under the 'Usage' section in the documentation, the default values for the
| 'mean' and 'sd' arguments to rnorm() are 0 and 1, respectively. Thus, rnorm(10)
| will generate 10 random numbers from a standard normal distribution. Give it a
| try.

> rnorm(10)
[1] 0.01435526 0.49670000 0.66719830 1.02615267 -0.17098444 1.17686717
[7] 0.56218999 -1.86215735 0.19312051 0.19784360

| All that hard work is paying off!

|==================================================== | 70%
| Now do the same, except with a mean of 100 and a standard deviation of 25.

> rnorm(100)
[1] 0.172982264 0.442130709 1.864432054 -0.563658921 -0.067282818 -1.851926229
[7] -0.556564192 2.269150370 -0.685331874 -0.805485164 0.335495327 -0.394761604
[13] -0.956868261 -0.010210579 -1.189646103 0.160903637 1.856238067 -1.130824834
[19] 0.702174906 -0.371508187 0.099294368 0.470084058 -0.934711088 -3.061130102
[25] -0.365677807 0.580680575 0.991796828 -0.244324909 -1.609483787 0.450058541
[31] 0.027285275 -0.336745830 -0.778377563 0.883549842 0.574392932 0.719208994
[37] -1.039703029 -0.475189910 -0.716451636 1.652040225 -0.260225182 -0.410915137
[43] -0.561969953 -1.043920112 1.321353245 0.376004793 -2.335380621 -1.687582759
[49] -0.464430671 -0.906059982 1.428993659 -0.011937050 -0.185916151 0.831380637
[55] 0.393130938 2.201306415 -0.482690652 0.315669653 0.830499039 1.603397355
[61] -0.770923661 0.483206820 -1.354875685 -0.229199596 0.776389972 0.824021565
[67] 0.003776438 -2.394822104 -1.692482547 0.531568093 1.039211524 -0.754086514
[73] -1.162728751 0.137876345 -1.790014210 1.266090940 0.955791981 0.863610915
[79] -0.170376789 1.504130968 0.683256893 0.458620053 -1.655138562 0.684304446
[85] 1.349127221 -1.727231759 1.086160568 -0.000491046 -0.250607431 0.059923930
[91] -0.944592227 -0.007397085 0.764051341 -0.683633702 -0.973472473 0.279404906
[97] 1.797531741 1.360690822 -1.723714186 0.828252276
| Not quite right, but keep trying. Or, type info() for more options.

| Use rnorm(10, mean = 100, sd = 25) to generate 10 random numbers from a normal
| distribution with mean 100 and standard deviation 25.

> rnorm(10, mean = 100, sd = 25)


[1] 138.77302 107.62040 100.26930 149.74431 74.55768 155.67540 75.76594 111.82567
[9] 139.96571 129.42430

| Excellent work!

|======================================================= | 73%
| Finally, what if we want to simulate 100 *groups* of random numbers, each
| containing 5 values generated from a Poisson distribution with mean 10? Let's
| start with one group of 5 numbers, then I'll show you how to repeat the operation
| 100 times in a convenient and compact way.

...

|========================================================= | 76%
| Generate 5 random values from a Poisson distribution with mean 10. Check out the
| documentation for rpois() if you need help.

> ?rpois
> ?rpois()
>
>
>
>
> rpois(mean(10))
Error in rpois(mean(10)) : argument "lambda" is missing, with no default
> info()

| When you are at the R prompt (>):


| -- Typing skip() allows you to skip the current question.
| -- Typing play() lets you experiment with R on your own; swirl will ignore what
| you do...
| -- UNTIL you type nxt() which will regain swirl's attention.
| -- Typing bye() causes swirl to exit. Your progress will be saved.
| -- Typing main() returns you to swirl's main menu.
| -- Typing info() displays these options again.

> swirl()

| Welcome to swirl! Please sign in. If you've been here before, use the same name as
| you did then. If you are new, call yourself something unique.

What shall I call you?


| Please don't use any quotes or other punctuation in your name.

What shall I call you? Anuja

| Would you like to continue with one of these lessons?

1: R Programming Simulation
2: No. Let me start something new.

Selection

Potrebbero piacerti anche