R Intermediate Introduction
R Intermediate is the depth pass over the beginner course. It takes familiar values like matrices and data frames further.
Totals for a matrix
The matrix holds two rows of sales values. rowSums adds each row into one total per row.
m <- matrix(c(4, 8, 2, 6), nrow = 2, byrow = TRUE)
print(m)
print(rowSums(m))A mean for a frame column
The frame pairs three names with three scores. mean averages the score column into one value.
df <- data.frame(name = c("Ana", "Bo", "Cy"), score = c(88, 92, 79))
print(df)
print(mean(df$score))Totals down the columns
The values fill a grid with two rows. colSums adds each column into one total per column.
m <- matrix(1:6, nrow = 2)
print(m)
print(colSums(m))Example
sales <- matrix(c(10, 20, 30, 40, 50, 60), nrow = 2, byrow = TRUE)
print(sales)
print(rowSums(sales))
print(colSums(sales))Key points
- Matrices get deeper indexing, true multiplication and summary functions.
- The apply family runs one function over rows, columns or groups.
- Environments and S3 objects add new ways to store values and pick behaviour.
- Dates and reshaping lessons cover time values and new table shapes.
- Every example uses base R and prints its result.
In this example, one matrix holds sales for two rows of three days. rowSums gives 60 and 150 for the rows and colSums gives 50, 70 and 90 for the days.
Try it Yourself
Edit the code, then press Run.
Running is simulated Β· rdrr.io R Snippets
Exercise
What does rowSums() return for a matrix?