<- c(3, -12, 532, 0, -100, 55, -42)
vec1
median(vec1)
[1] 0
min(vec1)
[1] -100
vec1
comprising the elements 3, -12, 532, 0, -100, 55,
and -42
. Then, find the median and lowest value in the vector.<- c(3, -12, 532, 0, -100, 55, -42)
vec1
median(vec1)
[1] 0
min(vec1)
[1] -100
vec2
which contains all the elements of vec1
that are positive integers. Then create a new vector vec3
that contains all the elements of vec1
that are negative integers. Then create a new vector vec4
which is the sum of vec2
and vec3
.<- vec1[vec1 > 0]
vec2 <- vec1[vec1 < 0]
vec3 <- vec2 + vec3 vec4
mtcars
to an object named after your favorite animal. Then calculate and print the median of the variable mpg
.# I'm a big donkey guy, but you can put your favorite animal for the name of the object.
<- mtcars
donkey
median(donkey$mpg)
[1] 19.2
mpg
and convert it to Kilometers per Liter, which you will name kpl
. This can be accomplished by taking mpg
and dividing it by 2.352. Once you’ve created this new variable and added it to the object, calculate the median of kpl
.$kpl <- donkey$mpg / 2.352
donkeymedian(donkey$kpl)
[1] 8.163265
OrchardSprays
to an object name of your choice. Then, convert the variable treatment
to an ordered factor variable, and change the existing names of factor levels from A:H to a list of sulphur levels from Sulpher_8
to Sulpher_1
. Then, print the new treatment
variable.library(dplyr)
# I'll assign the built-in OrchardSprays dataframe to an object called sprays.
<- OrchardSprays
sprays
# Next, I'll convert the treatment variable to an ordered factor variable.
$treatment <- factor(sprays$treatment,
spraysordered = TRUE)
summary(sprays$treatment)
A B C D E F G H
8 8 8 8 8 8 8 8
# Next, I'll replace the existing level names with new ones.
$treatment <- recode(sprays$treatment,
spraysA = "Sulphur_8",
B = "Sulphur_7",
C = "Sulphur_6",
D = "Sulphur_5",
E = "Sulphur_4",
F = "Sulphur_3",
G = "Sulphur_2",
H = "Sulphur_1")
print(sprays$treatment)
[1] Sulphur_5 Sulphur_4 Sulphur_7 Sulphur_1 Sulphur_2 Sulphur_3 Sulphur_6
[8] Sulphur_8 Sulphur_6 Sulphur_7 Sulphur_1 Sulphur_5 Sulphur_4 Sulphur_8
[15] Sulphur_3 Sulphur_2 Sulphur_3 Sulphur_1 Sulphur_8 Sulphur_4 Sulphur_5
[22] Sulphur_6 Sulphur_2 Sulphur_7 Sulphur_1 Sulphur_8 Sulphur_4 Sulphur_6
[29] Sulphur_3 Sulphur_2 Sulphur_7 Sulphur_5 Sulphur_4 Sulphur_5 Sulphur_2
[36] Sulphur_8 Sulphur_6 Sulphur_7 Sulphur_1 Sulphur_3 Sulphur_8 Sulphur_6
[43] Sulphur_3 Sulphur_2 Sulphur_7 Sulphur_5 Sulphur_4 Sulphur_1 Sulphur_7
[50] Sulphur_2 Sulphur_6 Sulphur_3 Sulphur_8 Sulphur_1 Sulphur_5 Sulphur_4
[57] Sulphur_2 Sulphur_3 Sulphur_5 Sulphur_7 Sulphur_1 Sulphur_4 Sulphur_8
[64] Sulphur_6
8 Levels: Sulphur_8 < Sulphur_7 < Sulphur_6 < Sulphur_5 < ... < Sulphur_1