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 vectorintakesKCal
of these four values.
What is the class of this vector?
Convert the values into in kJ using 1 kcal = 4.184 kJ.More dietary intakes. (Combining/appending/merging vectors.)
Additional set of intakes is provided: 2122, 2616, NA, 1771 kcal.
Usec()
to append the new intakes after values inintakesKCal
and store the result inallIntakesKCal
.
Print the combined vector and print its calculatedlength
.The average and total intakes. (Calculating means and sums, skipping missing values.)
Calculatemean
intake for patients in vectorintakesKCal
.
Next, calculatemean
intake for patients in vectorallIntakesKCal
.
Can you explain the result?
Check help for?mean
, in particular thena.rm
argument.
Use the extra argumentna.rm=TRUE
to calculate themean
of non-NA
elements ofallIntakesKCal
.
Check help for?sum
how to omitNA
elements in sum calculation.
Now, calculate the totalsum
ofallIntakesKCal
intakes ignoring theNA
element.Selecting valid intakes. (Selecting non-missing elements; logical vectors.)
Understand the result ofis.na( allIntakesKCal )
.
Now, negate the above result with!
operator.
Use above vectors as argument tosum
to calculate the number of missing and non-missing elements inallIntakesKCal
.
UnderstandallIntakesKCal[ !is.na( allIntakesKCal ) ]
.Generating random kcal intakes. (Generating normally distributed random numbers; descriptive statistics.)
The codev <- rnorm( 10 )
would sample 10 numbers from the normal distribution and store them as a vector inv
.
Printv
. Then repeatv <- rnorm( 10 )
and printv
again. Hasv
changed?
Next, read the manual ofrnorm
and find how to generate random numbers with givenmean
and standard deviation (sd
).
Now, inv
simulate kcal intake by generating 15 random numbers withmean=2000
andsd=300
.
Printv
and find by eye the smallest and the largest of these numbers.
Try to use the functionsmin
andmax
onv
– have you found the same numbers by eye?
Calculate themean
,median
and the standard deviation (sd
) ofv
.Selecting and counting some kcal intakes. (Selecting elements by a condition; logical vectors.)
Inv
simulate kcal intake by generating 15 random numbers withmean=2000
andsd=300
.
Typev < 2000
and understand the result.
How to interpret the number produced bysum( v < 2000 )
?
How to interpret the number produced bysum( !( v < 2000 ) )
?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 vectorv <- rnorm( 20 )
.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 vectorintakesKCal
of these eight values (in the given order).
Use the square brackets to get the 4th element ofintakesKCal
.
Use the square brackets and the colon operator (:
) to get the elements from the second to the fifth (inclusive).
Define another vectorposes
with values 1, 3, 5, 7. Use it get the 1st, 3rd, 5th and 7th element ofintakesKCal
.
Finally, get the 1st, 3rd, 5th and 7th element ofintakesKCal
typing numbers directly inside[...]
(without using an extraposes
variable).