Skip to content

Instantly share code, notes, and snippets.

View NyaGarcia's full-sized avatar
🐈

Nya NyaGarcia

🐈
View GitHub Profile
@NyaGarcia
NyaGarcia / rowSums.r
Created March 18, 2020 19:24
Calculating total values for rows with rowSums()
> rowSums(table(mtcars$gear, mtcars$cyl))
3 4 5
15 12 5
@NyaGarcia
NyaGarcia / str.r
Created March 18, 2020 19:57
Calling str() on a table object
> myCars<-table(mtcars$gear, mtcars$cyl)
> str(myCars)
'table' int [1:3, 1:3] 1 8 2 2 4 1 12 0 2
- attr(*, "dimnames")=List of 2
..$ : chr [1:3] "3" "4" "5"
..$ : chr [1:3] "4" "6" "8"
@NyaGarcia
NyaGarcia / rows.r
Created March 18, 2020 20:27
Selecting the first two rows of a table
> myCars[1:2,]
4 6 8
3 1 2 12
4 8 4 0
@NyaGarcia
NyaGarcia / row.r
Created March 18, 2020 20:31
Selecting the first row
> myCars[1,]
4 6 8
1 2 12
@NyaGarcia
NyaGarcia / rows-cols.r
Created March 18, 2020 20:37
Showing the first two rows of the first two columns
> myCars[1:2,1:2]
4 6
3 1 2
4 8 4
@NyaGarcia
NyaGarcia / named-row.r
Created March 18, 2020 20:39
Selecting the row named 3
> myCars["3",]
4 6 8
1 2 12
@NyaGarcia
NyaGarcia / named-col.r
Created March 18, 2020 20:41
Selecting the column named 8
> myCars[,"8"]
3 4 5
12 0 2
@NyaGarcia
NyaGarcia / threeway.r
Last active March 19, 2020 17:29
Three way contingency table with ftable()
> ftable(mtcars$cyl, mtcars$carb, mtcars$gear)
3 4 5
4 1 1 4 0
2 0 4 2
3 0 0 0
4 0 0 0
6 0 0 0
8 0 0 0
6 1 2 0 0
@NyaGarcia
NyaGarcia / fourway.r
Last active March 19, 2020 17:31
A four way contingency table using ftable()
> ftable(mtcars$cyl, mtcars$carb, mtcars$gear, mtcars$vs)
0 1
4 1 3 0 1
4 0 4
5 0 0
2 3 0 0
4 0 4
5 1 1
3 3 0 0
@NyaGarcia
NyaGarcia / mantelhaen.r
Created March 18, 2020 21:20
Testing for independence in a three way contingency table with Mantel-Haenszel Test
> mantelhaen.test(mtcars$cyl, mtcars$carb, mtcars$gear)
Cochran-Mantel-Haenszel test
data: mtcars$cyl and mtcars$carb and mtcars$gear
Cochran-Mantel-Haenszel M^2 = 30.212, df = 10, p-value = 0.0007908