forked from bioinformatics-core-shared-training/basicr
-
Notifications
You must be signed in to change notification settings - Fork 40
/
solution-exercise2.Rmd
76 lines (57 loc) · 1.84 KB
/
solution-exercise2.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
---
title: "Exercise 2"
author: "Mark Dunning"
date: '`r format(Sys.time(), "Last modified: %d %b %Y")`'
output: pdf_document
---
```{r}
age <- c(50, 21, 35, 45, 28, 31, 42, 33, 57, 62)
weight <- c(70.8, 67.9, 75.3, 61.9, 72.4, 69.9,
63.5, 71.5, 73.2, 64.8)
firstName <- c("Adam", "Eve", "John", "Mary",
"Peter", "Paul", "Joanna", "Matthew",
"David", "Sally")
secondName <- c("Jones", "Parker", "Evans", "Davis",
"Baker","Daniels", "Edwards", "Smith",
"Roberts", "Wilson")
consent <- c(TRUE, TRUE, FALSE, TRUE, FALSE,
FALSE, FALSE, TRUE, FALSE, TRUE)
sex <- c("Male", "Female", "Male", "Female", "Male",
"Male", "Female", "Male", "Male", "Female")
patients <- data.frame(First_Name = firstName,
Second_Name = secondName,
Full_Name = paste(firstName,
secondName),
Sex = factor(sex),
Age = age,
Weight = weight,
Consent = consent,
stringsAsFactors = FALSE)
patients
```
- Write R code to print the following subsets of the patients data frame
- The first and second rows, and the first and second colums
```{r}
patients[1:2,1:2]
```
- All rows, but in the order 10 to 1.
```{r}
patients[10:1,]
```
- All rows except the first row, all columns
```{r}
patients[-1,]
```
- Use logical indexing to select the following patients from the data frame:
1. Patients under 40
2. Patients who give consent to share their data
3. Men who weigh as much or more than the average European male (70.8 kg)
```{r}
patients[patients$Age < 40,]
```
```{r}
patients[patients$Consent,]
```
```{r}
patients[patients$Sex == "Male" & patients$Weight >= 70.8,]
```