Code
::opts_chunk$set(comment = ">")
knitrlibrary(tidyverse)
library(ggrepel)
library(ggthemes)
An integer
, because vectors can only be of one type and factors have an underlying numeric representation (as integers!).
You get 1
s and 0
s when you covert it to numeric
. You get "TRUE"
s and "FALSE"
s when you convert it to character.
You get "0"
s, and “1”
s if you convert it to numeric
and then character.
NA
s are technically a logical indicator of missingness so if an entire matrix is NA
, it’s data type will be logical
. NA
s can be paired with any data type so when the first column was added it automatically became the data type of those values. integers
are compatible with double
s so they were coerced to double
when the second column was added. All of the columns were coerced to a character
string when the character
and factor
vectors were added since characters are the lowest-common-denominator of data types. This progression is due to the fact that matrices can only be one data type so they will morph into whatever data type can coerce the others.
factor
s are technically represented by integers that correspond to their level number. Therefore, when vec_fac
was coerced into a character string, it was the numbers of the levels that became character
strings, not the level names themselves.
Add the argument byrow = TRUE
.
Firstly, the vector math_vec
is added to each column of math_matrix
. For the second part, the first calculation does matrix multiplication, while the second does element-wise multiplication.
> vec_char math_vec
> 1 A 1
> 2 B 2
> 3 C 3
> 4 D 4
> 5 E 5
> 6 F 1
> 7 G 2
> 8 H 3
> 9 I 4
> 10 J 5
math_vec
was repeated twice due to R
’s recycling rules for vectors.
> [1] "vec_char" "math_vec" "fac_letters"
> [1] "vec_char" "math_vec" "fac_letters"
> [1] "1" "2" "3" "4" "5" "6" "7" "8" "9" "10"
> [1] "letters" "math_matrix" "17" "vec_fac" "second_list"
> NULL
> NULL
> NULL
> NULL
> NULL
All of these functions work for a data.frame()
! names()
and colnames()
tell us the column names, while rownames()
gives us the numbers between 1 and 10. names()
works for lists and returns a vector of the names of each element. colnames()
and rownames()
don’t work for lists. None of these functions work for the matrix we created1.
length()
gives number of columns in a data frame, number of elements in a list, number of total cells in a matrix, and length of a vector. dim()
gives number of rows/columns for data frames and matrices, but doesn’t work for lists and vectors.
You can, however, assign row names and column names to a matrix using rownames()
and columnnames()
respectively. Alternatively, if you create a matrix using cbind
and named vectors, the colnames
will be taken from the names of the vectors used.↩︎