Professional Documents
Culture Documents
Applied Biostatistics 2020 - 02 The R Environment
Applied Biostatistics 2020 - 02 The R Environment
University College
Copenhagen
72 h Face-to-face learning;
84 h Directed learning;
84 h Autonomous learning
10 ECTS Points
Applied Biostatistics 2020 A. Parlesak 1
Global Nutrition & Health
The R Environment
Peter Dalgaard
Example:
if interest is compounded annually then n = 1
if interest is compounded quarterly then n = 4
if interest is compounded monthly then n = 12
More generally, an account that starts at $1, and yields (1+R) dollars
at simple interest, will yield eR dollars with continuous compounding.
http://www.algebralab.org/Word/Word.aspx?file=Algebra_InterestII.xml Applied Biostatistics 2020 A. Parlesak 10
Continuously Paid Interests
The formula to calculate continuously paid interests is:
• A = P ert
Applying the “order” function within the selection brackets returns the sorted
vector:
> xv_ord<-xv[order(xv)]
> xv_ord
[1] 4 9 21 34 67 76 89
Building in the ‘ordered=TRUE” extension to the factor call, you can fix the ranking of the levels:
> factor_increase_tax = factor(factor_increase_tax, ordered = TRUE, levels =
c("SD","D","I","A","SA"))
^- enforces hierarchy due to
> factor_increase_tax
sequence of the following
[1] A D D I SA SD I SA D D D SD I
concatenated elements
Levels: SD < D < I < A < SA
Applied Biostatistics 2020 A. Parlesak 15
Matrices and Dataframes in R
• Different vectors (irrespectively of the type of data) can be bound to give a
dataframe, which e.g. can contain all results from a study (e.g. the vectors
named “Age”, “Sex”, “SES”, “Height”, “Body_mass” etc.)
[data.frame(V1,V2,V3,…), cbind(V,DF) and rbind(V,DF)]
• The dataframe can have a name (“Met_Study”) and the vectors are then
called e.g. “Met_Study$Age”.
• An arranged order of vectors of the same data type (without headers) is
called matrix.
• If you wish to operate with the vector names only (without the name of the
dataframe before the vector), use the attach command (detach to
unspecify).
• A vector can (but does not have to) have a header, which can be
assigned with the “names” function.
> tail(mtcars)
mpg cyl disp hp drat wt qsec vs am gear carb
Porsche 914-2 26.0 4 120.3 91 4.43 2.140 16.7 0 1 5 2
Lotus Europa 30.4 4 95.1 113 3.77 1.513 16.9 1 1 5 2
Ford Pantera L 15.8 8 351.0 264 4.22 3.170 14.5 0 1 5 4
Ferrari Dino 19.7 6 145.0 175 3.62 2.770 15.5 0 1 5 6
Maserati Bora 15.0 8 301.0 335 3.54 3.570 14.6 0 1 5 8
Volvo 142E 21.4 4 121.0 109 4.11 2.780 18.6 1 1 4 2
• E.g. for the same dataframe, you can get an overview of the dataframe
structure with str():
> str(mtcars)
'data.frame': 32 obs. of 11 variables:
$ mpg : num 21 21 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 ...
$ cyl : num 6 6 4 6 8 6 8 4 4 6 ...
$ disp: num 160 160 108 258 360 ...
$ hp : num 110 110 93 110 175 105 245 62 95 123 ...
$ drat: num 3.9 3.9 3.85 3.08 3.15 2.76 3.21 3.69 3.92 3.92 ...
$ wt : num 2.62 2.88 2.32 3.21 3.44 ...
$ qsec: num 16.5 17 18.6 19.4 17 ...
$ vs : num 0 0 1 1 0 1 0 1 1 1 ...
$ am : num 1 1 1 0 0 0 0 0 0 0 ...
$ gear: num 4 4 4 3 3 3 3 4 4 4 ...
$ carb: num 4 4 1 1 2 1 4 2 2 4 ...
• In this case, the name of the dataframe corresponds to the file name (here:
ames), which is automtically attached (calls for vector name without the
name of the dataframe work).
• Assuming that you imported/created a data file in R that you’d like to save
is in the workspace.
• You can edit the resulting file with its given name (here: “present.txt”) with
any editing program (notepad, Excel, etc.).
You can save the whole actual workspace from R (including currently
active dataframes to disk with the call save:
• Histograms:
hist(DF$V)
• Plots of continuous-continuous interactions:
plot(DF$V1, DF$V2) or
plot(DF$V1~ DF$V2)
• Plots of categorial-continuous interactions:
boxplot(DF$V1~ DF$V2)
barplot(DF$V1~ DF$V2)
• Multiple chart generation
mosaicplot(DF$V1, DF$V2)
• You are requested to mix soy bean flour and corn (maize) flour to
give 1 kg of a final blend that contains the maximum amount of
protein, but does not exceed a total fat content of 9.5%.
• By which proportions do you have to mix the soy bean flour (fat:
20.7%, protein: 37.8%, USDA 16415) and corn (maize) flour (fat:
3.86%, protein: 9.28%, USDA 20019)?
• What is the final concentration of folate in the mixture? (soy flour:
365μg/100g, corn flour: 29μg/100g)