You are on page 1of 39

> akarpersamaankuadrat<-function(a,b,c){

+ x1=(-b+sqrt(b^2-4*1*c))/2*a
+ x1=(-b+sqrt(b^2-4*1*c))/2*a}
> akarpersamaankuadrat<-function(a,b,c){
+ x1=(-b+sqrt(b^2-4*a*c))/2*a
+ x2=(-b-sqrt(b^2-4*a*c))/2*a
+ hasil=cbind(x1,x2)
+ print(hasil)}
> akarpersamaankuadrat(1,-5,6)
x1 x2
[1,] 3 2

R version 3.1.2 (2014-10-31) -- "Pumpkin Helmet"


Copyright (C) 2014 The R Foundation for Statistical Computing
Platform: x86_64-w64-mingw32/x64 (64-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.

[Previously saved workspace restored]

> akarpersamaankuadrat<-function(a,b,c){
+ x1=(-b+sqrt(b^2-4*1*c))/2*a
+ x1=(-b+sqrt(b^2-4*1*c))/2*a}
> akarpersamaankuadrat<-function(a,b,c){
+ x1=(-b+sqrt(b^2-4*a*c))/2*a
+ x2=(-b-sqrt(b^2-4*a*c))/2*a
+ hasil=cbind(x1,x2)
+ print(hasil)}
> akarpersamaankuadrat(1,-5,6)
x1 x2
[1,] 3 2
> da> akarpersamaankuadrat<-function(a,b,c){
+ + x1=(-b+sqrt(b^2-4*1*c))/2*a
+ + x1=(-b+sqrt(b^2-4*1*c))/2*a}
Error in da > akarpersamaankuadrat <- function(a, b, c) { :
object 'da' not found
> > akarpersamaankuadrat<-function(a,b,c){
Error: unexpected '>' in ">"
> + x1=(-b+sqrt(b^2-4*a*c))/2*a
Error: object 'b' not found
> + x2=(-b-sqrt(b^2-4*a*c))/2*a
Error: object 'b' not found
> + hasil=cbind(x1,x2)
Error in cbind(x1, x2) : object 'x1' not found
> + print(hasil)}
Error: unexpected '}' in "+ print(hasil)}"

> > akarpersamaankuadrat(1,-5,6)


Error: unexpected '>' in ">"
>

x1 x2

Error: unexpected symbol in "

x1 x2"

> [1,] 3 2

> data<-c(1,2,3,4,4,5,7,9,8,6,2,2,1,0)
> ratarata<-function(x){
+ rata=sum(x)/length(x)
+ return(rata)}
> ratarata(data)
[1] 3.857143
> mean(data)
[1] 3.857143
> varian<-function(x){
+ var=(sum(x-ratarata(x))^2)/length(x-1)
+ return(var)}
> varian(data)
[1] 2.253888e-31
> var(data)
[1] 7.824176
> varian<-function(x){
+ rata=sum(x)/length(x)
+ var=(sum(x-rata)^2)/length(x-1)
+ print(var)}
> varian(data)
[1] 2.253888e-31
> inversperkalianmatriks<-function(x,y){

+ perkalian=x &*& y
Error: unexpected '*' in:
"inversperkalianmatriks<-function(x,y){
perkalian=x &*"
> inverperkalianmatriks<-function(x,y){
+ perkalian= x &*& y
Error: unexpected '*' in:
"inverperkalianmatriks<-function(x,y){
perkalian= x &*"
> inversperkalianmatrik<-function(x,y){
+ matrik1=matrix(x,2,3)
+ matrik2=matrix(y,4,2)
+ perkalian=matrik1 &*& matrik2
Error: unexpected '*' in:
"matrik2=matrix(y,4,2)
perkalian=matrik1 &*"
> inversperkalianmatrik<-function(x,y){
+ matrik2=matrix(y,4,2)
+ matrik1=matrix(x,2,3)
+ perkalian=matrik1 &* matrik2
Error: unexpected '*' in:
"matrik1=matrix(x,2,3)
perkalian=matrik1 &*"
> a=c(1,2,3)
> b=c(2,3,4)
> matrik1=matrix(a,1,3)
> matrik2=matrix(b,3,1)
> perkalian=matrik1 &*& matrik2

Error: unexpected '*' in "perkalian=matrik1 &*"


> perkalian=matrix(matrik1 &*& matrik2)
Error: unexpected '*' in "perkalian=matrix(matrik1 &*"
> matrik1 &*& matrik2
Error: unexpected '*' in "matrik1 &*"
> perkalianmatrik<-function(x,y){
+}
> perkalian= matrik1& * &matrik2
Error: unexpected '*' in "perkalian= matrik1& *"
> perkalian=matrik1*matrik2
Error in matrik1 * matrik2 : non-conformable arrays
> matrik1
[,1] [,2] [,3]
[1,]

> matrik2
[,1]
[1,]

[2,]

[3,]

> matrik1& * &matrik2


Error: unexpected '*' in "matrik1& *"
> matrik1 &*& matrik2
Error: unexpected '*' in "matrik1 &*"
> matrik1 * matrik2
Error in matrik1 * matrik2 : non-conformable arrays
> perkalian=matrik1%*%matrik2
> perkalian
[,1]

[1,] 20
> inversperkalianmatrik<-function(x,y){
+ x=matrix(x,2,4)
+ y=matrix(y,4,2)
+ perkalian=x %*% y
+ inverss=solve(perkalian)
+ print(inverss)}
> x=c(1,2,4,3,5,2,1,1)
> y=c(1,3,3,1,2,1,1,2)
> inversperkalianmatrik(x,y)
[,1]

[,2]

[1,] 0.1294118 -0.1529412


[2,] -0.2117647 0.3411765
>x
[1] 1 2 4 3 5 2 1 1
> matrix(x,2,4)
[,1] [,2] [,3] [,4]
[1,]

[2,]

> x=c(1,5,2,2,4,1,3,1)
> y=c(1,3,2,1,3,1,1,2)
> inversperkalianmatrik(x,y)
[,1] [,2]
[1,] 0.13333333 -0.10
[2,] -0.09333333 0.12
> da> x=c(1,5,2,2,4,1,3,1)
Error in da > x = c(1, 5, 2, 2, 4, 1, 3, 1) : object 'da' not found
> > y=c(1,3,2,1,3,1,1,2)

Error: unexpected '>' in ">"


> > inversperkalianmatrik(x,y)
Error: unexpected '>' in ">"
>

[,1] [,2]

Error: unexpected '[' in "

["

> [1,] 0.13333333 -0.10


Error: unexpected '[' in "["
> [2,] -0.09333333 0.12
Error: unexpected '[' in "["
> datamatrix=matrix(rnorm(100,5,3),ncol=5)
> datamatrix
[,1]

[,2]

[,3]

[,4]

[,5]

[1,] 10.2479130 5.912812 7.00863588 4.700220 2.4482923


[2,] 6.3687866 9.676478 5.82660141 5.809922 1.7881081
[3,] 0.9733241 4.616768 5.64682370 3.819071 4.1620140
[4,] 3.5938875 2.627095 7.82558600 9.748018 6.7833029
[5,] 8.4443325 0.463488 3.60196832 0.940163 5.3333510
[6,] 6.7464542 3.932334 8.08040757 5.241727 2.7469054
[7,] 9.5968416 8.157011 4.84199904 3.019844 5.9736667
[8,] -2.1477922 6.654144 2.97259705 10.631458 3.5149726
[9,] -2.7277185 3.581181 8.70140655 3.573425 3.7674736
[10,] 5.3984853 2.250580 5.41987916 3.188341 8.9003944
[11,] 5.3120896 13.997422 6.41293084 4.993891 5.4294605
[12,] 1.8555174 1.917679 4.35122471 7.100027 9.0839938
[13,] 2.4885754 3.743254 4.53592455 6.786673 5.3772744
[14,] 0.3285015 8.486955 2.85666356 7.327109 3.2089464
[15,] 9.6177102 6.457394 -0.07146617 4.064202 0.3058527
[16,] 4.0090893 4.946180 3.46326206 6.111604 2.6453613

[17,] 0.4126741 6.135236 4.43827286 2.634083 4.1362764


[18,] 3.3710133 3.224288 3.73478773 4.829941 13.5432265
[19,] 4.5151875 10.894066 4.37843005 1.061396 0.9105817
[20,] 7.8339561 1.621213 4.70730122 2.747879 1.3189295
> rataratabaris<-function(x){
+}
> tes <- rnorm(100) tes <- matrix(rnorm(100), ncol=1) colMeans(matrix(tes[,1],
nrow=5))
Error: unexpected symbol in "tes <- rnorm(100) tes"
> tes <- rnorm(100) tes <- matrix(rnorm(100), ncol=1) colMeans(matrix(tes[,1],
nrow=5))
Error: unexpected symbol in "tes <- rnorm(100) tes"
> tes <- rnorm(100)
> tes <- matrix(rnorm(100), ncol=1) colMeans(matrix(tes[,1], nrow=5))
Error: unexpected symbol in "tes <- matrix(rnorm(100), ncol=1) colMeans"
> colMeans(matrix(tes[,1], nrow=5))
Error in tes[, 1] : incorrect number of dimensions
> rates <- rnorm(100) tes <- matrix(rnorm(100), ncol=1)
colMeans(matrix(tes[,1], nrow=5))

> datamatrix=matrix(rnorm(100,5,3), ncol=5)


> for (i in 1:n){
+ sum(i)/length(i)}
Error: object 'n' not found
> for (i in 1:n) {
+ n<-i*10
+ m[1,i]<-n
+ sum(i)/length(i)}
Error: object 'n' not found

> ratarata<-function(x){
+ sum(x)/length(x)
+}
> ratarata<-function(x){
+ rata=sum(x)/length(x)
+ print(rata)}
> apply(datamatrix,1,ratarata)
[1] 6.929432
[1] 6.802456
[1] 6.519798
[1] 7.929591
[1] 5.531118
[1] 5.587691
[1] 7.726662
[1] 3.96771
[1] 4.458315
[1] 6.016545
[1] 3.027016
[1] 3.281215
[1] 8.32629
[1] 4.29969
[1] 3.827778
[1] 7.281742
[1] 6.401858
[1] 6.324347
[1] 2.752315
[1] 4.154752
[1] 6.929432 6.802456 6.519798 7.929591 5.531118 5.587691 7.726662
3.967710

[9] 4.458315 6.016545 3.027016 3.281215 8.326290 4.299690 3.827778


7.281742
[17] 6.401858 6.324347 2.752315 4.154752
> mean(10.2479130,5.912812,7.00863588,4.700220,2.4482923)
[1] 10.24791
> b<-c(10.2479130,5.912812,7.00863588,4.700220,2.4482923)
> mean(b)
[1] 6.063575
> n<-100
> for(i in 1:n){
+ datamatrix<-matrix(rnorm(100,5,3),ncol=5)
+ m[i,1]<-n
+ m[i,2]<-mean(datamatrix)}
Error in m[i, 1] <- n : object 'm' not found
> n<-100
> m<-matrix(0,n,2)
> datamatrix<-matrix(rnorm(100,5,3),ncol=5)
> m<-matrix(0,n,2)}
Error: unexpected '}' in "m<-matrix(0,n,2)}"
> n<-100
> m<-matrix(0,n,2)
> for(i in 1:n){
+ datamatrix<-matrix(rnorm(100,5,3),ncol=5)
+ m[i,1]<-n
+ m[i,2]<-mean(datamatrix)}
> plot(m[,1],m[,2],type='1',xlab='N',ylab='rata-rata',main='judul grafik')
Error in plot.xy(xy, type, ...) : invalid plot type '1'
> matrix(0,n,2)
[,1] [,2]

[1,]

[2,]

[3,]

[4,]

[5,]

[6,]

[7,]

[8,]

[9,]

[10,]

[11,]

[12,]

[13,]

[14,]

[15,]

[16,]

[17,]

[18,]

[19,]

[20,]

[21,]

[22,]

[23,]

[24,]

[25,]

[26,]

[27,]

[28,]

[29,]

[30,]

[31,]

[32,]

[33,]

[34,]

[35,]

[36,]

[37,]

[38,]

[39,]

[40,]

[41,]

[42,]

[43,]

[44,]

[45,]

[46,]

[47,]

[48,]

[49,]

[50,]

[51,]

[52,]

[53,]

[54,]

[55,]

[56,]

[57,]

[58,]

[59,]

[60,]

[61,]

[62,]

[63,]

[64,]

[65,]

[66,]

[67,]

[68,]

[69,]

[70,]

[71,]

[72,]

[73,]

[74,]

[75,]

[76,]

[77,]

[78,]

[79,]

[80,]

[81,]

[82,]

[83,]

[84,]

[85,]

[86,]

[87,]

[88,]

[89,]

[90,]

[91,]

[92,]

[93,]

[94,]

[95,]

[96,]

[97,]

[98,]

[99,]

[100,]

> n<-100
> m<-matrix(rnorm(100,5,3),n,5)
> for (i in 1:n){
+ datamatrix<-matrix(rnorm(100,5,3),ncol=5)
+ m[1,i]<-n
+ rata=sum(n)/length(n)
+ print(rata)}
[1] 100
[1] 100
[1] 100
[1] 100
[1] 100

Error in `[<-`(`*tmp*`, 1, i, value = 100) : subscript out of bounds


> datamatrix=matrix(rnorm(100,5,3),ncol=5)
> for (i in seq(datamatrix[1,]){
Error: unexpected '{' in "for (i in seq(datamatrix[1,]){"
> datamatrix=matrix(rnorm(100,5,3),ncol=5)
> for (i in seq(datamatrix[1,])
+{
Error: unexpected '{' in:
"for (i in seq(datamatrix[1,])
{"
> datamatrix=matrix(rnorm(100,5,3),ncol=5)
> for (i in seq(datamatrix[1,])
+ rata=sum(i)/length(i)
Error: unexpected symbol in:
"for (i in seq(datamatrix[1,])
rata"
> datamatrix=matrix(rnorm(100,5,3),ncol=5)
> for (i in seq(datamatrix[1,])){
+ rata=sum(i)/length(i)
+ print(i)}
[1] 1
[1] 2
[1] 3
[1] 4
[1] 5
> datamatrix=matrix(rnorm(100,5,3),ncol=5)
> for (i in seq(datamatrix[1,])){
+ rata=sum(i)/length(i)

+ print(rata)}
[1] 1
[1] 2
[1] 3
[1] 4
[1] 5
> datamatrix=matrix(rnorm(100,5,3),ncol=5)
> for (i in seq(along(datamatrix[1,]))){
+ rata=sum(datamatrix[i])/length(i)
+ print(rata)}
Error in seq(along(datamatrix[1, ])) : could not find function "along"
> datamatrix=matrix(rnorm(100,5,3),ncol=5)
> n<-datamatrix[1,]
> for (i in seq(along(n)){
Error: unexpected '{' in "for (i in seq(along(n)){"
> for (i in seq(along(n))){
+ rata=sum(n[i])/length(n)
+ print(rata)}
Error in seq(along(n)) : could not find function "along"
> for (i in seq_along=n){
Error: unexpected '=' in "for (i in seq_along="
> datamatrix=matrix(rnorm(100,5,3),ncol=5)
> n<-datamatrix[1,]
> ratabaris<-function(n){
+ rata=sum(n)/length(n)
+ print(rata)}
> ratabaris
function(n){

rata=sum(n)/length(n)
print(rata)}
> ratabaris(datamatrix)
[1] 5.114462
> ratabaris<-function(x){
+ rata=sum(x)/length(x)
+ print(rata)}
> ratabaris(n)
[1] 4.499662
> daVectors
Error: object 'daVectors' not found
>
> Try R is Sponsored By:
Error: unexpected symbol in "Try R"
>
> O'Reilly
+ Complete to
+ UnlockChapter 2 Badge
+ The name may sound intimidating, but a vector is simply a list of values. R
relies on vectors for many of its operations. This includes basic plots - we'll have
you drawing graphs by the end of this chapter (and it's a lot easier than you
might think)!
Error: unexpected string constant in:
"UnlockChapter 2 Badge
The name may sound intimidating, but a vector is simply a list of values. R relies
on vectors for many of its operations. This includes basic plots - we'"
>
> Course tip: if you haven't already, try clicking on the expand icon (Expand
Sidebar) in the upper-left corner of the sidebar. The expanded sidebar offers a
more in-depth look at chapter sections and progress.
Error: unexpected symbol in "Course tip"

>
> Vectors 2.1
Error: unexpected numeric constant in "Vectors 2.1"
>
> A vector's values can be numbers, strings, logical values, or any other type, as
long as they're all the same type. Try creating a vector of numbers, like this:
Error: unexpected symbol in "A vector"
>
> RedoComplete
Error: object 'RedoComplete' not found
> > c(4, 7, 9)
Error: unexpected '>' in ">"
> [1] 4 7 9
Error: unexpected '[' in "["
> The c function (c is short for Combine) creates a new vector by combining a
list of values.
Error: unexpected symbol in "The c"
>
> Now try creating a vector with strings:
Error: unexpected symbol in "Now try"
>
> RedoComplete
Error: object 'RedoComplete' not found
> > c('a', 'b', 'c')
Error: unexpected '>' in ">"
> [1] "a" "b" "c"
Error: unexpected '[' in "["
> Vectors cannot hold values with different modes (types). Try mixing modes and
see what happens:

Error: unexpected symbol in "Vectors cannot"


>
> RedoComplete
Error: object 'RedoComplete' not found
> > c(1, TRUE, "three")
Error: unexpected '>' in ">"
> [1] "1"

"TRUE" "three"

Error: unexpected '[' in "["


> All the values were converted to a single mode (characters) so that the vector
can hold them all.
Error: unexpected symbol in "All the"
>
> Sequence Vectors 2.2
Error: unexpected symbol in "Sequence Vectors"
>
> If you need a vector with a sequence of numbers you can create it with
start:end notation. Let's make a vector with values from 5 through 9:
Error: unexpected symbol in "If you"
>
> RedoComplete
Error: object 'RedoComplete' not found
> > 5:9
Error: unexpected '>' in ">"
> [1] 5 6 7 8 9
Error: unexpected '[' in "["
> A more versatile way to make sequences is to call the seq function. Let's do
the same thing with seq:
Error: unexpected symbol in "A more"
>

> RedoComplete
Error: object 'RedoComplete' not found
> > seq(5, 9)
Error: unexpected '>' in ">"
> [1] 5 6 7 8 9
Error: unexpected '[' in "["
> seq also allows you to use increments other than 1. Try it with steps of 0.5:
Error: unexpected symbol in "seq also"
>
> RedoComplete
Error: object 'RedoComplete' not found
> > seq(5, 9, 0.5)
Error: unexpected '>' in ">"
> [1] 5.0 5.5 6.0 6.5 7.0 7.5 8.0 8.5 9.0
Error: unexpected '[' in "["
> Now try making a vector with integers from 9 down to 5:
Error: unexpected symbol in "Now try"
>
> RedoComplete
Error: object 'RedoComplete' not found
> > 9:5
Error: unexpected '>' in ">"
> [1] 9 8 7 6 5
Error: unexpected '[' in "["
> Vector Access 2.3
Error: unexpected symbol in "Vector Access"
>
> We're going to create a vector with some strings in it for you, and store it in
the sentence variable.

+
+ You can retrieve an individual value within a vector by providing its numeric
index in square brackets. Try getting the third value:
+
+ RedoComplete
+ > sentence <- c('walk', 'the', 'plank')
Error: unexpected string constant in:
"RedoComplete
> sentence <- c('"
> > sentence[3]
Error: unexpected '>' in ">"
> [1] "plank"
Error: unexpected '[' in "["
> Many languages start array indices at 0, but R's vector indices start at 1. Get
the first value by typing:
Error: unexpected symbol in "Many languages"
>
> RedoComplete
Error: object 'RedoComplete' not found
> > sentence[1]
Error: unexpected '>' in ">"
> [1] "walk"
Error: unexpected '[' in "["
> You can assign new values within an existing vector. Try changing the third
word to "dog":
Error: unexpected symbol in "You can"
>
> RedoComplete
Error: object 'RedoComplete' not found

> > sentence[3] <- "dog"


Error: unexpected '>' in ">"
> If you add new values onto the end, the vector will grow to accommodate
them. Let's add a fourth word:
Error: unexpected symbol in "If you"
>
> RedoComplete
Error: object 'RedoComplete' not found
> > sentence[4] <- 'to'
Error: unexpected '>' in ">"
> You can use a vector within the square brackets to access multiple values. Try
getting the first and third words:
Error: unexpected symbol in "You can"
>
> RedoComplete
Error: object 'RedoComplete' not found
> > sentence[c(1, 3)]
Error: unexpected '>' in ">"
> [1] "walk" "dog"
Error: unexpected '[' in "["
> This means you can retrieve ranges of values. Get the second through fourth
words:
Error: unexpected symbol in "This means"
>
> RedoComplete
Error: object 'RedoComplete' not found
> > sentence[2:4]
Error: unexpected '>' in ">"
> [1] "the" "dog" "to"

Error: unexpected '[' in "["


> You can also set ranges of values; just provide the values in a vector. Add
words 5 through 7:
Error: unexpected symbol in "You can"
>
> RedoComplete
Error: object 'RedoComplete' not found
> > sentence[5:7] <- c('the', 'poop', 'deck')
Error: unexpected '>' in ">"
> Now try accessing the sixth word of the sentence vector:
Error: unexpected symbol in "Now try"
>
> RedoComplete
Error: object 'RedoComplete' not found
> > sentence[6]
Error: unexpected '>' in ">"
> [1] "poop"
Error: unexpected '[' in "["
> Vector Names 2.4
Error: unexpected symbol in "Vector Names"
>
> For this challenge, we'll make a 3-item vector for you, and store it in the ranks
variable.
Error: unexpected symbol in "For this"
>
> You can assign names to a vector's elements by passing a second vector filled
with names to the names assignment function, like this:
Error: unexpected symbol in "You can"
>

> RedoComplete
Error: object 'RedoComplete' not found
> > ranks <- 1:3
Error: unexpected '>' in ">"
> > names(ranks) <- c("first", "second", "third")
Error: unexpected '>' in ">"
> Assigning names for a vector can act as useful labels for the data. Below, you
can see what our vector looks like now.
Error: unexpected symbol in "Assigning names"
>
> You can also use the names to access the vector's values. Try getting the value
for the "first" rank:
Error: unexpected symbol in "You can"
>
> RedoComplete
Error: object 'RedoComplete' not found
> > ranks
Error: unexpected '>' in ">"
> first second third
Error: unexpected symbol in " first second"
>

Error: unexpected numeric constant in "


> > ranks["first"]
Error: unexpected '>' in ">"
> first
Error: object 'first' not found
>
[1] 1

2"

> Now set the current value for the "third" rank to a different value using the
name rather than the position.
Error: unexpected symbol in "Now set"
>
> RedoComplete
Error: object 'RedoComplete' not found
> > ranks["third"] <- 4
Error: unexpected '>' in ">"
> Plotting One Vector 2.5
Error: unexpected symbol in "Plotting One"
>
> The barplot function draws a bar chart with a vector's values. We'll make a
new vector for you, and store it in the vesselsSunk variable.
Error: unexpected symbol in "The barplot"
>
> Now try passing the vector to the barplot function:
Error: unexpected symbol in "Now try"
>
> RedoComplete
Error: object 'RedoComplete' not found
> > vesselsSunk <- c(4, 5, 1)
Error: unexpected '>' in ">"
> > barplot(vesselsSunk)
Error: unexpected '>' in ">"
>0
[1] 0
>1
[1] 1
>2

[1] 2
>3
[1] 3
>4
[1] 4
>5
[1] 5
> If you assign names to the vector's values, R will use those names as labels on
the bar plot. Let's use the names assignment function again:
Error: unexpected symbol in "If you"
>
> RedoComplete
Error: object 'RedoComplete' not found
> > names(vesselsSunk) <- c("England", "France", "Norway")
Error: unexpected '>' in ">"
> Now, if you call barplot with the vector again, you'll see the labels:
Error: unexpected ',' in "Now,"
>
> RedoComplete
Error: object 'RedoComplete' not found
> > barplot(vesselsSunk)
Error: unexpected '>' in ">"
> England
Error: object 'England' not found
> France
Error: object 'France' not found
> Norway
Error: object 'Norway' not found
>0

[1] 0
>1
[1] 1
>2
[1] 2
>3
[1] 3
>4
[1] 4
>5
[1] 5
> Now, try calling barplot on a vector of integers ranging from 1 through 100:
Error: unexpected ',' in "Now,"
>
> RedoComplete
Error: object 'RedoComplete' not found
> > barplot(1:100)
Error: unexpected '>' in ">"
>0
[1] 0
> 20
[1] 20
> 40
[1] 40
> 60
[1] 60
> 80
[1] 80

> 100
[1] 100
> Vector Math 2.6
Error: unexpected symbol in "Vector Math"
>
> Most arithmetic operations work just as well on vectors as they do on single
values. We'll make another sample vector for you to work with, and store it in the
a variable.
Error: unexpected symbol in "Most arithmetic"
>
> If you add a scalar (a single value) to a vector, the scalar will be added to each
value in the vector, returning a new vector with the results. Try adding 1 to each
element in our vector:
Error: unexpected symbol in "If you"
>
> RedoComplete
Error: object 'RedoComplete' not found
> > a <- c(1, 2, 3)
Error: unexpected '>' in ">"
>>a+1
Error: unexpected '>' in ">"
> [1] 2 3 4
Error: unexpected '[' in "["
> The same is true of division, multiplication, or any other basic arithmetic. Try
dividing our vector by 2:
Error: unexpected symbol in "The same"
>
> RedoComplete
Error: object 'RedoComplete' not found
>>a/2

Error: unexpected '>' in ">"


> [1] 0.5 1.0 1.5
Error: unexpected '[' in "["
> Now try multiplying our vector by 2:
Error: unexpected symbol in "Now try"
>
> RedoComplete
Error: object 'RedoComplete' not found
> > a*2
Error: unexpected '>' in ">"
> [1] 2 4 6
Error: unexpected '[' in "["
> If you add two vectors, R will take each value from each vector and add them.
We'll make a second vector for you to experiment with, and store it in the b
variable.
Error: unexpected symbol in "If you"
>
> Try adding it to the a vector:
Error: unexpected symbol in "Try adding"
>
> RedoComplete
Error: object 'RedoComplete' not found
> > b <- c(4, 5, 6)
Error: unexpected '>' in ">"
>>a+b
Error: unexpected '>' in ">"
> [1] 5 7 9
Error: unexpected '[' in "["
> Now try subtracting b from a:

Error: unexpected symbol in "Now try"


>
> RedoComplete
Error: object 'RedoComplete' not found
> > a-b
Error: unexpected '>' in ">"
> [1] -3 -3 -3
Error: unexpected '[' in "["
> You can also take two vectors and compare each item. See which values in the
a vector are equal to those in a second vector:
Error: unexpected symbol in "You can"
>
> RedoComplete
Error: object 'RedoComplete' not found
> > a == c(1, 99, 3)
Error: unexpected '>' in ">"
> [1] TRUE FALSE TRUE
Error: unexpected '[' in "["
> Notice that R didn't test whether the whole vectors were equal; it checked
each value in the a vector against the value at the same index in our new vector.
Error: unexpected symbol in "Notice that"
>
> Check if each value in the a vector is less than the corresponding value in
another vector:
Error: unexpected 'if' in "Check if"
>
> RedoComplete
Error: object 'RedoComplete' not found
> > a<c(1,99,3)

Error: unexpected '>' in ">"


> [1] FALSE TRUE FALSE
Error: unexpected '[' in "["
> Functions that normally work with scalars can operate on each element of a
vector, too. Try getting the sine of each value in our vector:
Error: unexpected symbol in "Functions that"
>
> RedoComplete
Error: object 'RedoComplete' not found
> > sin(a)
Error: unexpected '>' in ">"
> [1] 0.8414710 0.9092974 0.1411200
Error: unexpected '[' in "["
> Now try getting the square roots with sqrt:
Error: unexpected symbol in "Now try"
>
> RedoComplete
Error: object 'RedoComplete' not found
> > sqrt(a)
Error: unexpected '>' in ">"
> [1] 1.000000 1.414214 1.732051
Error: unexpected '[' in "["
> Scatter Plots 2.7
Error: unexpected symbol in "Scatter Plots"
>
> The plot function takes two vectors, one for X values and one for Y values, and
draws a graph of them.
Error: unexpected symbol in "The plot"
>

> Let's draw a graph showing the relationship of numbers and their sines.
+
+ First, we'll need some sample data. We'll create a vector for you with some
fractional values between 0 and 20, and store it in the x variable.
Error: unexpected string constant in:
"
First, we'"
>
> Now, try creating a second vector with the sines of those values:
Error: unexpected ',' in "Now,"
>
> RedoComplete
Error: object 'RedoComplete' not found
> > x <- seq(1, 20, 0.1)
Error: unexpected '>' in ">"
> > y <- sin(x)
Error: unexpected '>' in ">"
> Then simply call plot with your two vectors:
Error: unexpected symbol in "Then simply"
>
> RedoComplete
Error: object 'RedoComplete' not found
> > plot(x, y)
Error: unexpected '>' in ">"
> Great job! Notice on the graph that values from the first argument (x) are used
for the horizontal axis, and values from the second (y) for the vertical.
Error: unexpected symbol in "Great job"
>
>5

[1] 5
> 10
[1] 10
> 15
[1] 15
> 20
[1] 20
> -1.0
[1] -1
> -0.5
[1] -0.5
> 0.0
[1] 0
> 0.5
[1] 0.5
> 1.0
[1] 1
>x
[1] 1 5 2 2 4 1 3 1
>y
[1] 1 3 2 1 3 1 1 2
> Your turn. We'll create a vector with some negative and positive values for you,
and store it in the values variable.
Error: unexpected symbol in "Your turn."
>
> We'll also create a second vector with the absolute values of the first, and
store it in the absolutes variable.
+

+ Try plotting the vectors, with values on the horizontal axis, and absolutes on
the vertical axis.
+
+ RedoComplete
+ > values <- -10:10
+ > absolutes <- abs(values)
+ > plot(values, absolutes)
+ -10
+ -5
+0
+5
+ 10
+0
+2
+4
+6
+8
+ 10
+ values
+ absolutes
+ NA Values 2.8
+
+ Sometimes, when working with sample data, a given value isn't available. But
it's not a good idea to just throw those values out. R has a value that explicitly
indicates a sample was not available: NA. Many functions that work with vectors
treat this value specially.
Error: unexpected string constant in:
"
Sometimes, when working with sample data, a given value isn'"
>

> We'll create a vector for you with a missing sample, and store it in the a
variable.
+
+ Try to get the sum of its values, and see what the result is:
+
+ RedoComplete
+ > a <- c(1, 3, NA, 7, 9)
+ > sum(a)
+ [1] NA
+ The sum is considered "not available" by default because one of the vector's
values was NA. This is the responsible thing to do; R won't just blithely add up
the numbers without warning you about the incomplete data. We can explicitly
tell sum (and many other functions) to remove NA values before they do their
calculations, however.
Error: unexpected string constant in:
"[1] NA
The sum is considered "not available" by default because one of the vector'"
>
> Remember that command to bring up help for a function? Bring up
documentation for the sum function:
Error: unexpected symbol in "Remember that"
>
> RedoComplete
Error: object 'RedoComplete' not found
> > help(sum)
Error: unexpected '>' in ">"
> sum

package:base

Error: unexpected symbol in "sum


>
> Sum of Vector Elements
Error: unexpected symbol in "Sum of"

R Documentation
package"

>
> Description:
+

'sum' returns the sum of all the values present in its arguments.

Error: unexpected symbol in:


"Description:
'sum' returns"
>
> Usage:
+

sum(..., na.rm = FALSE)

Error: object 'Usage' not found


> ...
Error: '...' used in an incorrect context
> As you see in the documentation, sum can take an optional named argument,
na.rm. It's set to FALSE by default, but if you set it to TRUE, all NA arguments will
be removed from the vector before the calculation is performed.
Error: unexpected symbol in "As you"
>
> Try calling sum again, with na.rm set to TRUE:
Error: unexpected symbol in "Try calling"
>
> RedoComplete
Error: object 'RedoComplete' not found
> > sum(a, na.rm = TRUE)
Error: unexpected '>' in ">"
> [1] 20
Error: unexpected '[' in "["
> Chapter 2 Completed
Error: unexpected numeric constant in "Chapter 2"
>

> Chapter 2 Badge


Error: unexpected numeric constant in "Chapter 2"
> Share your plunder:
Error: unexpected symbol in "Share your"
>
> You've traversed Chapter 2 and discovered another badge! If you'd like to
add this to your Code School account, click here to log in over on Code School, if
you'd like to add it to your account.
Error: unexpected string constant in "You've traversed Chapter 2 and
discovered another badge! If you'"
>
> In this chapter, we've shown you all the basics of manipulating vectors creating and accessing them, doing math with them, and making sequences.
We've shown you how to make bar plots and scatter plots with vectors. And
we've shown you how R treats vectors where one or more values are not
available.
Error: unexpected symbol in "In this"
>
> The vector is just the first of several data structures that R offers. See you in
the next chapter, where we'll talk about the matrix.
Error: unexpected symbol in "The vector"
>
> More from O'Reilly
Error: unexpected symbol in "More from"
>
> Did you know that our sponsor O'Reilly has some great resources for big data
practitioners? Check out the Strata Newsletter, the Strata Blog, and get access to
five e-books on big data topics from leading thinkers in the space.a;lfsd;lsdl;
Error: unexpected symbol in "Did you"
> datamatrix<-matrix(rnorm(100,5,3),ncol=5)
> ratabaris<-function(x)
+{

+ rata=sum(x)/length(x)
+ print(rata)}
> ratabarismatriks<-function(x){
+ n=datamatrix[,i]
+ for (i in 1:n){
+ rata=sum(n[i])/length(n)
+ print(rata)}
+}
> ratabarismatriks(datamatrix)
[1] 0.1888829
[1] 0.173873
[1] 0.2841222
Warning message:
In 1:n : numerical expression has 20 elements: only the first used
> datamatrix<-matrix(rnorm(100,5,3),ncol=5)
> ratabarismatriks<-function(x){
+ for (i in datamatrix[,1]){
+

> for (i in datamatrix[,1]){


+}
> for (i in datamatrix[1,]){
+ colMeans(matrix(datamatrix[,1],nrow=20))
+ print()}
Error in print.default() : argument "x" is missing, with no default
> datamatrix<-matrix(rnorm(100,5,3),ncol=5)
> datamatrix
[,1]

[,2]

[,3]

[,4]

[,5]

[1,] 5.5134362 5.7551725 4.7213432 5.0353705 5.709782


[2,] 5.8536326 5.7679282 3.9484884 4.6887322 4.110796
[3,] 3.0313596 5.2799012 3.8734785 0.1513157 6.669709
[4,] 1.6990076 4.7397666 -2.6020575 5.3865750 6.729660
[5,] 4.4117390 4.0979509 1.9023282 7.4907351 2.681587
[6,] 4.3047192 7.7856150 4.9411151 8.8180844 7.483222
[7,] 4.0815285 3.7023443 5.1482330 5.1801763 6.845403
[8,] 2.5691416 1.0305409 3.1997069 5.2137654 5.120345
[9,] 0.7430962 1.0836193 10.0500662 5.1622562 1.236009
[10,] -0.1179184 7.4423037 2.6900017 5.5163477 2.363668
[11,] 4.7859472 4.1468849 5.0800735 9.5148339 6.211780
[12,] 4.8905065 9.6060546 11.3782552 5.2674916 11.837360
[13,] 5.3137122 0.4724132 4.4066610 6.0513564 4.320132
[14,] 6.3488980 3.6808404 5.4825235 8.0768575 3.563820
[15,] 2.3628312 4.0252415 6.0849460 8.0501054 6.180326
[16,] 3.7978480 2.4023994 9.6035887 5.6150304 5.405374
[17,] 4.1855146 1.7810105 5.7056693 4.7815447 3.236081
[18,] 2.2493129 8.3890491 6.8825334 2.8033944 7.578008
[19,] 0.8660072 2.4821894 8.8298803 6.2405519 4.468486
[20,] 10.2764116 5.0251302 0.5077886 4.6027103 6.483329
> ratabaris<-function(x){
+ while (i <= nrow(x)){
+ sum(x[i,])/length(x)
+}
+}
> ratabaris(datamatrix)

You might also like