Primary exercises
- Dietary intakes. (Create a vector, use it in
calculation.)
Four patients had daily dietary intakes of 2314, 2178, 1922, 2004
kcal.
Make a vector intakesKCal
of these four values.
What is the class of this vector?
Convert the values into in kJ using 1 kcal = 4.184 kJ.
intakesKCal <- c( 2314, 2178, 1922, 2004 )
intakesKCal
[1] 2314 2178 1922 2004
class( intakesKCal )
[1] "numeric"
intakesKCal * 4.184
[1] 9681.776 9112.752 8041.648 8384.736
- More dietary intakes. (Combining/appending/merging
vectors.)
Additional set of intakes is provided: 2122, 2616, NA, 1771 kcal.
Use c()
to append the new intakes after values in
intakesKCal
and store the result in
allIntakesKCal
.
Print the combined vector and print its calculated
length
.
intakesKCal2 <- c( 2122, 2616, NA, 1771 )
allIntakesKCal <- c( intakesKCal, intakesKCal2 )
allIntakesKCal
[1] 2314 2178 1922 2004 2122 2616 NA 1771
length( allIntakesKCal )
[1] 8
- The average and total intakes. (Calculating means and sums,
skipping missing values.)
Calculate mean
intake for patients in vector
intakesKCal
.
Next, calculate mean
intake for patients in vector
allIntakesKCal
.
Can you explain the result?
Check help for ?mean
, in particular the na.rm
argument.
Use the extra argument na.rm=TRUE
to calculate the
mean
of non-NA
elements of
allIntakesKCal
.
Check help for ?sum
how to omit NA
elements in
sum calculation.
Now, calculate the total sum
of allIntakesKCal
intakes ignoring the NA
element.
mean( intakesKCal )
[1] 2104.5
mean( allIntakesKCal )
[1] NA
# since one element is missing, the mean is unknown
# ?mean, adding argument na.rm=TRUE will omit NA elements
mean( allIntakesKCal, na.rm = TRUE )
[1] 2132.429
# ?sum also allows na.rm=TRUE argument to skip NA elements
sum( allIntakesKCal, na.rm = TRUE )
[1] 14927
- Selecting valid intakes. (Selecting non-missing elements;
logical vectors.)
Understand the result of is.na( allIntakesKCal )
.
Now, negate the above result with !
operator.
Use above vectors as argument to sum
to calculate the
number of missing and non-missing elements in
allIntakesKCal
.
Understand allIntakesKCal[ !is.na( allIntakesKCal ) ]
.
is.na( allIntakesKCal ) # TRUE marks positions with missing data
[1] FALSE FALSE FALSE FALSE FALSE FALSE TRUE FALSE
!is.na( allIntakesKCal ) # TRUE marks positions with available data
[1] TRUE TRUE TRUE TRUE TRUE TRUE FALSE TRUE
sum( is.na( allIntakesKCal ) ) # number of missing elements
[1] 1
sum( !is.na( allIntakesKCal ) ) # number of non-missing elements
[1] 7
allIntakesKCal[ !is.na( allIntakesKCal ) ] # keeps elements which are not NA
[1] 2314 2178 1922 2004 2122 2616 1771
sum( allIntakesKCal[ !is.na( allIntakesKCal ) ] ) # same as sum( allIntakesKCal, na.rm = TRUE )
[1] 14927
- Generating random kcal intakes. (Generating normally distributed
random numbers; descriptive statistics.)
The code v <- rnorm( 10 )
would sample 10 numbers from
the normal distribution and store them as a vector in
v
.
Print v
. Then repeat v <- rnorm( 10 )
and
print v
again. Has v
changed?
Next, read the manual of rnorm
and find how to generate
random numbers with given mean
and standard deviation
(sd
).
Now, in v
simulate kcal intake by generating 15 random
numbers with mean=2000
and sd=300
.
Print v
and find by eye the smallest and the largest of
these numbers.
Try to use the functions min
and max
on
v
– have you found the same numbers by eye?
Calculate the mean
, median
and the standard
deviation (sd
) of v
.
v <- rnorm( 10 ) # a vector of random numbers
v
[1] -0.60849420 0.61770697 -0.29127865 0.24411340 1.13502863 1.05630482 -0.59378357 -0.01008851
[9] 0.25549299 -0.94839076
v <- rnorm( 10 ) # another vector of random numbers
v
[1] -2.89889280 -0.36966930 -0.02092365 0.16963431 0.25755862 -0.02919566 0.24020212 -0.71659878
[9] 0.02088286 -0.53527995
v <- rnorm( n = 15, mean = 2000, sd = 300 )
v
[1] 2493.864 2135.970 2019.281 1707.820 2208.042 2290.242 2084.313 2199.477 2375.511 2122.941
[11] 2043.861 2299.513 1811.992 2197.615 2462.411
min( v )
[1] 1707.82
max( v )
[1] 2493.864
mean( v ) # is it close to 2000? try several random v vectors and see the effect of growing n
[1] 2163.523
median( v )
[1] 2197.615
sd( v ) # is it close to 300? try several random v vectors and see the effect of growing n
[1] 216.7009
- Selecting and counting some kcal intakes. (Selecting elements by
a condition; logical vectors.)
In v
simulate kcal intake by generating 15 random numbers
with mean=2000
and sd=300
.
Type v < 2000
and understand the result.
How to interpret the number produced by
sum( v < 2000 )
?
How to interpret the number produced by
sum( !( v < 2000 ) )
?
v <- rnorm( n = 15, mean = 2000, sd = 300 )
v
[1] 1991.132 1774.307 2353.657 2218.017 1937.369 1866.065 2528.703 2238.502 1729.036 1927.612
[11] 2617.384 2042.802 1567.063 2204.715 2194.593
v < 2000 # TRUE corresponds to elements of vector v SMALLER THAN 2000
[1] TRUE TRUE FALSE FALSE TRUE TRUE FALSE FALSE TRUE TRUE FALSE FALSE TRUE FALSE FALSE
v[ v < 2000 ] # selected elements of v smaller than 2000
[1] 1991.132 1774.307 1937.369 1866.065 1729.036 1927.612 1567.063
sum( v < 2000 ) # number of elements in vector v smaller than 2000
[1] 7
sum( !( v < 2000 ) ) # number of elements in vector v GREATER OR EQUAL than 2000
[1] 8
sum( v >= 2000 ) # same as above
[1] 8
- Head and tail.
Often there is a need to quickly look at the beginning
(head
) or at the end (tail
) of a vector.
Try these functions to show the first 5 and the last 7 elements of a
randomly generated vector v <- rnorm( 20 )
.
v <- rnorm( 20 )
v
[1] -0.009879784 0.446962671 0.890397829 0.909817412 -1.438963806 -0.037423945 0.101027758
[8] -0.048002027 1.000931356 -0.341106083 -0.502743252 -1.371341346 -1.430643919 -0.995084511
[15] 2.505658712 0.445523307 0.425937937 -0.061130833 -0.065026535 -0.090551237
head( v, 5 )
[1] -0.009879784 0.446962671 0.890397829 0.909817412 -1.438963806
tail( v, 7 )
[1] -0.99508451 2.50565871 0.44552331 0.42593794 -0.06113083 -0.06502654 -0.09055124
- Elements of a vector.
Let’s assume that eight persons had caloric intakes of 2122, 2616, NA,
1771, 2314, 2178, 1922, 2004 kcal.
Make a vector intakesKCal
of these eight values (in the
given order).
Use the square brackets to get the 4th element of
intakesKCal
.
Use the square brackets and the colon operator (:
) to get
the elements from the second to the fifth (inclusive).
Define another vector poses
with values 1, 3, 5, 7. Use it
get the 1st, 3rd, 5th and 7th element of intakesKCal
.
Finally, get the 1st, 3rd, 5th and 7th element of
intakesKCal
typing numbers directly inside
[...]
(without using an extra poses
variable).
intakesKCal <- c( 2122, 2616, NA, 1771, 2314, 2178, 1922, 2004 )
intakesKCal
[1] 2122 2616 NA 1771 2314 2178 1922 2004
intakesKCal[ 4 ]
[1] 1771
intakesKCal[ 2:5 ]
[1] 2616 NA 1771 2314
poses <- c(1,3,5,7)
intakesKCal[ poses ]
[1] 2122 NA 2314 1922
intakesKCal[ c(1,3,5,7) ]
[1] 2122 NA 2314 1922