R語言從基礎入門到提高(四)matrices(矩陣)
阿新 • • 發佈:2019-01-06
# Box office Star Wars (in millions!)
new_hope <- c(460.998, 314.4)
empire_strikes <- c(290.475, 247.900)
return_jedi <- c(309.306, 165.8)
# Construct matrix
star_wars_matrix <- matrix(c(new_hope, empire_strikes, return_jedi), nrow = 3, byrow = TRUE)
#mark#重點理解
#這個地方已經把整個矩陣繪出來啦,只需指明 行,列名稱。注意瞭解幾行幾列 !!!
# Vectors region and titles, used for naming
region <- c("US", "non-US")
titles <- c("A New Hope", "The Empire Strikes Back", "Return of the Jedi")
# Name the columns with region
colnames( star_wars_matrix ) <- region
#對star_wars_matrix 指明列名 ,把region 向量賦給它
# Name the rows with titles
rownames( star_wars_matrix ) <- titles
#對star_wars_matrix 指明行名 ,把titles 向量賦給它
# Print out star_wars_matrix
star_wars_matrix
console:
> # Box office Star Wars (in millions!)
> new_hope <- c(460.998, 314.4)
> empire_strikes <- c(290.475, 247.900)
> return_jedi <- c(309.306, 165.8)
>
> # Construct matrix
> star_wars_matrix <- matrix(c(new_hope, empire_strikes, return_jedi), nrow = 3, byrow = TRUE)
>
> # Vectors region and titles, used for naming
> region <- c("US", "non-US")
> titles <- c("A New Hope", "The Empire Strikes Back", "Return of the Jedi")
>
> # Name the columns with region
> colnames( star_wars_matrix ) <- region
>
> # Name the rows with titles
> rownames( star_wars_matrix ) <- titles
>
> # Print out star_wars_matrix
>
> star_wars_matrix
US non-US
A New Hope 460.998 314.4
The Empire Strikes Back 290.475 247.9
Return of the Jedi 309.306 165.8