Introduction to R Matrices answer
来源:互联网 发布:淘宝店铺自己做活动 编辑:程序博客网 时间:2024/06/15 19:20
What’s a matrix?
# Construct a matrix with 3 rows that contain the numbers 1 up to 9matrix(1:9,byrow=T,nrow=3)
Analyzing matrices, you shall
# 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)# Create box_officebox_office <-c(new_hope, empire_strikes, return_jedi) # Construct star_wars_matrixstar_wars_matrix <-matrix(box_office,nrow=3,byrow=T)
Naming a matrix
# 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 matrixstar_wars_matrix <- matrix(c(new_hope, empire_strikes, return_jedi), nrow = 3, byrow = TRUE)# Vectors region and titles, used for namingregion <- c("US", "non-US")titles <- c("A New Hope", "The Empire Strikes Back", "Return of the Jedi")# Name the columns with regioncolnames(star_wars_matrix)<-region# Name the rows with titlesrownames(star_wars_matrix)<-titles# Print out star_wars_matrixstar_wars_matrix
Calculating the worldwide box office
# Construct star_wars_matrixbox_office <- c(460.998, 314.4, 290.475, 247.900, 309.306, 165.8)star_wars_matrix <- matrix(box_office, nrow = 3, byrow = TRUE, dimnames = list(c("A New Hope", "The Empire Strikes Back", "Return of the Jedi"), c("US", "non-US")))# Calculate worldwide box office figuresworldwide_vector <-rowSums(star_wars_matrix)
Adding a column for the Worldwide box office
# Construct star_wars_matrixbox_office <- c(460.998, 314.4, 290.475, 247.900, 309.306, 165.8)star_wars_matrix <- matrix(box_office, nrow = 3, byrow = TRUE, dimnames = list(c("A New Hope", "The Empire Strikes Back", "Return of the Jedi"), c("US", "non-US")))# The worldwide box office figuresworldwide_vector <- rowSums(star_wars_matrix)# Bind the new variable worldwide_vector as a column to star_wars_matrixall_wars_matrix <-cbind(star_wars_matrix,worldwide_vector)
Adding a row
# star_wars_matrix and star_wars_matrix2 are available in your workspacestar_wars_matrix star_wars_matrix2 # Combine both Star Wars trilogies in one matrixall_wars_matrix <- rbind(star_wars_matrix,star_wars_matrix2)
The total box office revenue for the entire saga
# all_wars_matrix is available in your workspaceall_wars_matrix# Total revenue for US and non-UStotal_revenue_vector <-colSums(all_wars_matrix) # Print out total_revenue_vectortotal_revenue_vector
Selection of matrix elements
# all_wars_matrix is available in your workspaceall_wars_matrix# Select the non-US revenue for all moviesnon_us_all <-all_wars_matrix[,2]# Average non-US revenuemean(non_us_all)# Select the non-US revenue for first two moviesnon_us_some <- all_wars_matrix[c(1,2),2]# Average non-US revenue for first two moviesmean(non_us_some)
A little arithmetic with matrices
# all_wars_matrix is available in your workspaceall_wars_matrix# Estimate the visitorsvisitors <-all_wars_matrix/5 # Print the estimate to the consolevisitors
A little arithmetic with matrices (2)
# all_wars_matrix and ticket_prices_matrix are available in your workspaceall_wars_matrixticket_prices_matrix# Estimated number of visitorsvisitors <-all_wars_matrix/ticket_prices_matrix# US visitorsus_visitors <-visitors[,1] # Average number of US visitorsprint(mean(us_visitors))
阅读全文
0 0
- Introduction to R Matrices answer
- Introduction to R Intro to basics answer
- Introduction to Vectors answer
- Datacamp Notes: Introduction to R
- Introduction to LabKey and R Integration
- 学习Introduction to Data Analysis using R系列
- R语言dplyr包介绍 introduction to dplyr
- answer to mirror
- Answer To Reset---ATR
- Introduction of R Code
- Introduction to Scientific Programing and Simulation Using R chapter 04 答案
- The answer to the question
- How to concatenate two matrices in Python?
- [R] creating matrices from vectors by adding a dimension attribute
- R tutorial 08 - Advance Matrices 进阶函数-阵列
- Introduction to CGI Variables
- Introduction to Rave Reports
- Introduction to RSS
- Intellij idea 使用问题收集
- 类模板
- CSS兼容性详解
- 进程和线程的关系
- Android Service
- Introduction to R Matrices answer
- python:Process、join()、进程的创建Process子类、进程池Pool、将一个文件夹的内容copy到另一个文件夹
- Linux下的eval以及和$(),``
- 百练_2687:数组逆序重放
- Lintcode 空格替换
- hyperledger fabric交易流程
- 怎么让动态的 iframe 高度自适应里面内容
- 1.删除一个无头单链表的非尾节点 2.从尾到头打印单链表
- angular4.0 ERROR Error: Uncaught (in promise): Error: No provider for AuthGuard! Error: No provider