2+3
## [1] 5
sqrt(36)
## [1] 6
log10(1000)
## [1] 3
log2(32)
## [1] 5
x
and print
its value.x=2+3+4
x
## [1] 9
5 - 145
.abs(5-145)
## [1] 140
x
.x=sqrt(625)/5
x
## [1] 5
vec
vec=c(1:5, 10)
vec
## [1] 1 2 3 4 5 10
vec
.length(vec)
## [1] 6
[ ]
function.a = c("a", "b", "c", "d", "e","f", "g", "h")
a[5:6]
## [1] "e" "f"
x
, that contains the numbers 2, 3, 4,
5 and y
, that contains 100, 200, 300 and 400. What happens
if you try to multiply them?x = 2:5
y = c(100, 200, 300, 400)
x
## [1] 2 3 4 5
y
## [1] 100 200 300 400
x*y
## [1] 200 600 1200 2000
z
that contains the concatenation
result of x
and y
.z = c(x,y)
z
## [1] 2 3 4 5 100 200 300 400
w
by extracting all elements from
x
except the last onew = x[-1]
w
(“one” and “two”) and
extract the “one” element.names(w) = c("one", "two")
w["one"]
## one
## 3
g
that contains numbers from 1 to 20:
g
.g
printing the
vector after each step:Add 2 to the first two elements
Multiply the elements from the 16th to the 20th by 30
Compute the square root of the elements from the 3rd to the 6th
Compute the logarithm (base 10) of all elements except the 9th
Divide by 2 the elements from the 10th to the 12th
Multiply the 15th element by pi number
Assign names to the elements of g
:
"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T"
Raise elements “C”, “D” and “O” to the 5th power
Subtract 10 from elements “H”, “M” and “R”
Compute the absolute value of all elements except “M” and “S”
At the end, evaluate the length of g
, its
type
(Is it the same as before?) and its
class
.
Create a new vector g2
that has the same elements as
g
but with reverse order.
Hint: use g
and the :
(what happend if you
type 20:1
?)
Create a new vector g3
by multiplying g
by g2
.
Use a logical vector (TRUE and FALSE) to extract the 3rd, 6th,
and 9th elements of g3
Hint: Use rep()
to
create the logical vector and store the result in a new vector
g4
.
Divide g
by g4
and store the result in
g5
. What happens? How long is g5
?
Concatenate all the vectors (g, g2, g3, g4, g5
) into
a new vector g6
. How many elements does g6
contain?
g = 1:20
g
## [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
typeof(g)
## [1] "integer"
g[1:2] = g[1:2]+2
g
## [1] 3 4 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
g[16:20] = g[16:20]*30
g
## [1] 3 4 3 4 5 6 7 8 9 10 11 12 13 14 15 480 510 540 570
## [20] 600
g[3:6] = sqrt(g[3:6])
g
## [1] 3.000000 4.000000 1.732051 2.000000 2.236068 2.449490
## [7] 7.000000 8.000000 9.000000 10.000000 11.000000 12.000000
## [13] 13.000000 14.000000 15.000000 480.000000 510.000000 540.000000
## [19] 570.000000 600.000000
g[-9] = log10(g[-9])
g
## [1] 0.4771213 0.6020600 0.2385606 0.3010300 0.3494850 0.3890756 0.8450980
## [8] 0.9030900 9.0000000 1.0000000 1.0413927 1.0791812 1.1139434 1.1461280
## [15] 1.1760913 2.6812412 2.7075702 2.7323938 2.7558749 2.7781513
g[10:12] = g[10:12]/2
g
## [1] 0.4771213 0.6020600 0.2385606 0.3010300 0.3494850 0.3890756 0.8450980
## [8] 0.9030900 9.0000000 0.5000000 0.5206963 0.5395906 1.1139434 1.1461280
## [15] 1.1760913 2.6812412 2.7075702 2.7323938 2.7558749 2.7781513
g[15] = g[15]*pi
g
## [1] 0.4771213 0.6020600 0.2385606 0.3010300 0.3494850 0.3890756 0.8450980
## [8] 0.9030900 9.0000000 0.5000000 0.5206963 0.5395906 1.1139434 1.1461280
## [15] 3.6947997 2.6812412 2.7075702 2.7323938 2.7558749 2.7781513
names(g) = c("A", "B", "C", "D", "E","F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T")
g
## A B C D E F G H
## 0.4771213 0.6020600 0.2385606 0.3010300 0.3494850 0.3890756 0.8450980 0.9030900
## I J K L M N O P
## 9.0000000 0.5000000 0.5206963 0.5395906 1.1139434 1.1461280 3.6947997 2.6812412
## Q R S T
## 2.7075702 2.7323938 2.7558749 2.7781513
g[c( "C", "D", "O")]=g[c( "C", "D", "O")]^5
g
## A B C D E F
## 4.771213e-01 6.020600e-01 7.726696e-04 2.472002e-03 3.494850e-01 3.890756e-01
## G H I J K L
## 8.450980e-01 9.030900e-01 9.000000e+00 5.000000e-01 5.206963e-01 5.395906e-01
## M N O P Q R
## 1.113943e+00 1.146128e+00 6.885801e+02 2.681241e+00 2.707570e+00 2.732394e+00
## S T
## 2.755875e+00 2.778151e+00
g[c("H", "M","R")] = g[c("H", "M","R")]-10
g
## A B C D E
## 4.771213e-01 6.020600e-01 7.726696e-04 2.472002e-03 3.494850e-01
## F G H I J
## 3.890756e-01 8.450980e-01 -9.096910e+00 9.000000e+00 5.000000e-01
## K L M N O
## 5.206963e-01 5.395906e-01 -8.886057e+00 1.146128e+00 6.885801e+02
## P Q R S T
## 2.681241e+00 2.707570e+00 -7.267606e+00 2.755875e+00 2.778151e+00
g[-c(13, 19)] = abs(g[-c(13, 19)])
g
## A B C D E
## 4.771213e-01 6.020600e-01 7.726696e-04 2.472002e-03 3.494850e-01
## F G H I J
## 3.890756e-01 8.450980e-01 9.096910e+00 9.000000e+00 5.000000e-01
## K L M N O
## 5.206963e-01 5.395906e-01 -8.886057e+00 1.146128e+00 6.885801e+02
## P Q R S T
## 2.681241e+00 2.707570e+00 7.267606e+00 2.755875e+00 2.778151e+00
length(g)
## [1] 20
typeof(g)
## [1] "double"
class(g)
## [1] "numeric"
g2 = g[20:1]
g2
## T S R Q P
## 2.778151e+00 2.755875e+00 7.267606e+00 2.707570e+00 2.681241e+00
## O N M L K
## 6.885801e+02 1.146128e+00 -8.886057e+00 5.395906e-01 5.206963e-01
## J I H G F
## 5.000000e-01 9.000000e+00 9.096910e+00 8.450980e-01 3.890756e-01
## E D C B A
## 3.494850e-01 2.472002e-03 7.726696e-04 6.020600e-01 4.771213e-01
g3 = g*g2
g3
## A B C D E
## 1.325515010 1.659201992 0.005615459 0.006693120 0.937053600
## F G H I J
## 267.909737321 0.968590557 -80.835657695 4.856315607 0.260348171
## K L M N O
## 0.260348171 4.856315607 -80.835657695 0.968590557 267.909737321
## P Q R S T
## 0.937053600 0.006693120 0.005615459 1.659201992 1.325515010
g4 = g3[c(rep(FALSE, 2), TRUE, rep(FALSE, 2), TRUE,rep(FALSE, 2), TRUE,rep(FALSE, 11))]
g4
## C F I
## 5.615459e-03 2.679097e+02 4.856316e+00
g5 = g/g4
## Warning in g/g4: longer object length is not a multiple of shorter object
## length
g5
## A B C D E
## 8.496568e+01 2.247249e-03 1.591061e-04 4.402138e-01 1.304488e-03
## F G H I J
## 8.011745e-02 1.504949e+02 3.395513e-02 1.853257e+00 8.903992e+01
## K L M N O
## 1.943551e-03 1.111111e-01 -1.582428e+03 4.278038e-03 1.417906e+02
## P Q R S T
## 4.774750e+02 1.010628e-02 1.496527e+00 4.907658e+02 1.036973e-02
length(g5)
## [1] 20
g6 = c(g,g2, g3, g4, g5)
g6
## A B C D E
## 4.771213e-01 6.020600e-01 7.726696e-04 2.472002e-03 3.494850e-01
## F G H I J
## 3.890756e-01 8.450980e-01 9.096910e+00 9.000000e+00 5.000000e-01
## K L M N O
## 5.206963e-01 5.395906e-01 -8.886057e+00 1.146128e+00 6.885801e+02
## P Q R S T
## 2.681241e+00 2.707570e+00 7.267606e+00 2.755875e+00 2.778151e+00
## T S R Q P
## 2.778151e+00 2.755875e+00 7.267606e+00 2.707570e+00 2.681241e+00
## O N M L K
## 6.885801e+02 1.146128e+00 -8.886057e+00 5.395906e-01 5.206963e-01
## J I H G F
## 5.000000e-01 9.000000e+00 9.096910e+00 8.450980e-01 3.890756e-01
## E D C B A
## 3.494850e-01 2.472002e-03 7.726696e-04 6.020600e-01 4.771213e-01
## A B C D E
## 1.325515e+00 1.659202e+00 5.615459e-03 6.693120e-03 9.370536e-01
## F G H I J
## 2.679097e+02 9.685906e-01 -8.083566e+01 4.856316e+00 2.603482e-01
## K L M N O
## 2.603482e-01 4.856316e+00 -8.083566e+01 9.685906e-01 2.679097e+02
## P Q R S T
## 9.370536e-01 6.693120e-03 5.615459e-03 1.659202e+00 1.325515e+00
## C F I A B
## 5.615459e-03 2.679097e+02 4.856316e+00 8.496568e+01 2.247249e-03
## C D E F G
## 1.591061e-04 4.402138e-01 1.304488e-03 8.011745e-02 1.504949e+02
## H I J K L
## 3.395513e-02 1.853257e+00 8.903992e+01 1.943551e-03 1.111111e-01
## M N O P Q
## -1.582428e+03 4.278038e-03 1.417906e+02 4.774750e+02 1.010628e-02
## R S T
## 1.496527e+00 4.907658e+02 1.036973e-02
length(g6)
## [1] 83