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 vectorintakesKCalof 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 inintakesKCaland store the result inallIntakesKCal.
Print the combined vector and print its calculatedlength.The average and total intakes. (Calculating means and sums, skipping missing values.)
Calculatemeanintake for patients in vectorintakesKCal.
Next, calculatemeanintake for patients in vectorallIntakesKCal.
Can you explain the result?
Check help for?mean, in particular thena.rmargument.
Use the extra argumentna.rm=TRUEto calculate themeanof non-NAelements ofallIntakesKCal.
Check help for?sumhow to omitNAelements in sum calculation.
Now, calculate the totalsumofallIntakesKCalintakes ignoring theNAelement.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 tosumto 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 printvagain. Hasvchanged?
Next, read the manual ofrnormand find how to generate random numbers with givenmeanand standard deviation (sd).
Now, invsimulate kcal intake by generating 15 random numbers withmean=2000andsd=300.
Printvand find by eye the smallest and the largest of these numbers.
Try to use the functionsminandmaxonv– have you found the same numbers by eye?
Calculate themean,medianand the standard deviation (sd) ofv.Selecting and counting some kcal intakes. (Selecting elements by a condition; logical vectors.)
Invsimulate kcal intake by generating 15 random numbers withmean=2000andsd=300.
Typev < 2000and 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 vectorintakesKCalof 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 vectorposeswith 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 ofintakesKCaltyping numbers directly inside[...](without using an extraposesvariable).