Professional Documents
Culture Documents
+ 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
> 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)}"
x1 x2
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
> matrik2
[,1]
[1,]
[2,]
[3,]
[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]
[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)
[,1] [,2]
["
[,2]
[,3]
[,4]
[,5]
> 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
[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
+ 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:
"TRUE" "three"
> 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
> 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"
>
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
> 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
R Documentation
package"
>
> Description:
+
'sum' returns the sum of all the values present in its arguments.
+ 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]){
+
[,2]
[,3]
[,4]
[,5]