Skip to content

Commit

Permalink
Merge pull request #113 from ARTbio/IOC_R_improve
Browse files Browse the repository at this point in the history
tweak content for week4
  • Loading branch information
bellenger-l authored Mar 8, 2024
2 parents da03d77 + 5cad655 commit 8ea80aa
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 26 deletions.
9 changes: 4 additions & 5 deletions docs/R-IOC/05_IOC_R_week_04.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,10 @@ name the rows with "r1", "r2", "r3" and the columns with "c1", "c2", "c3", "c4".
- [x] 8. Replace the element bigger than 10 by 99 in `my_mat`.
- [x] 9. Transforme the matrix `my_mat` to a `data.frame` named `my_df`.
- [x] 10. Use the rownames to create a new column "id" for `my_df`.
- [x] 11. Which row(s) has(have) duplicated values in `my_df`?
- [x] 12. Create a new column named "total" in `my_df`, which calculates the sum of column "c1" to "c4" by row.
- [x] 13. Change the column order to put the "id" in the first column in `my_df`.
- [x] 14. Remove the rownames of `my_df`.
- [x] 15. Add a new row in `my_df` which contains the sum of each column (except the "id" column, put `NA` in the new row for this column).
- [x] 11. Create a new column named "total" in `my_df`, which calculates the sum of column "c1" to "c4" by row.
- [x] 12. Change the column order to put the "id" in the first column in `my_df`.
- [x] 13. Remove the rownames of `my_df`.
- [x] 14. Add a new row in `my_df` which contains the sum of each column (except the "id" column, put `NA` in the new row for this column).

Please be aware of the best practices for your Rscript, we will be attentive to them!

Expand Down
61 changes: 40 additions & 21 deletions docs/R-IOC/r06_df_matrices.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,18 +128,17 @@ X
## [2,] 2 4 -2
## [3,] -3 -2 16

Y <- matrix(0:8, ncol = 3)
Y
## [,1] [,2] [,3]
## [1,] 0 3 6
## [2,] 1 4 7
## [3,] 2 5 8
colSums(X) # calculate the sum by column
## [1] 8 4 11

X %*% Y # matrix multiplication
## [,1] [,2] [,3]
## [1,] -4 20 44
## [2,] 0 12 24
## [3,] 30 63 96
rowSums(X) # calculate the sum by row
## [1] 8 4 11

colMeans(X) # calculate the average by column
## [1] 2.666667 1.333333 3.666667

rowMeans(X) # calculate the average by row
## [1] 2.666667 1.333333 3.666667

t(X) # matrix transpose
## [,1] [,2] [,3]
Expand All @@ -159,17 +158,18 @@ solve(X) # inverse X
diag(X) # matrix diagonals
## [1] 9 4 16

colSums(X)
## [1] 8 4 11

rowSums(X)
## [1] 8 4 11

colMeans(X)
## [1] 2.666667 1.333333 3.666667
Y <- matrix(0:8, ncol = 3)
Y
## [,1] [,2] [,3]
## [1,] 0 3 6
## [2,] 1 4 7
## [3,] 2 5 8

rowMeans(X)
## [1] 2.666667 1.333333 3.666667
X %*% Y # matrix multiplication
## [,1] [,2] [,3]
## [1,] -4 20 44
## [2,] 0 12 24
## [3,] 30 63 96
```

### To Go Further
Expand Down Expand Up @@ -390,6 +390,25 @@ my_df
## sample5 5 27 male cohort1
```

Remember the built-in functions which calculate the sums or the average by column or by row seen in matrix's part?
They can be used on data frame for numeric rows or columns too!

```r
colSums(my_df[, c("id", "age")])
## id age
## 15 126

colMeans(my_df[, c("id", "age")])
## id age
## 3.0 25.2

rowSums(my_df[, c("id", "age")])
## [1] 22 27 21 39 32

rowMeans(my_df[, c("id", "age")])
## [1] 11.0 13.5 10.5 19.5 16.0
```

For other possible manipulations in `matrix` and `data.frame`, please refer to the sections [8.3 to 8.6](https://bookdown.org/ndphillips/YaRrr/matrix-and-dataframe-functions.html) of Philips’ book.

---

0 comments on commit 8ea80aa

Please sign in to comment.