FIT1043 · Introduction to Data Science
Introduction to R for Data Science
Week 8 of Monash FIT1043 Introduction to Data Science switches tools to R and RStudio, the unit's second analysis language, and repeats the earlier skills in R idiom: vectors and data frames, summary statistics, reading large/compressed files, fitting a linear model with lm(), and plotting. It also introduces dplyr verbs (group_by, summarise, filter) and factors for derived categories. This chapter is the backbone of Data Science Assignment 2, whose analysis stage is done in R, and code-reading in R is examinable the same way pandas is.
What this chapter covers
- 01R and RStudio / RStudio Cloud; R as the second most popular data-analytics language
- 02R syntax basics: assignment with <-, vectorised operations (element-wise), c() to build vectors, sequences 1:10
- 03Random data generation: runif (uniform), rnorm (normal), sample()
- 04Summary statistics: mean(), max(), min(), sd(), summary(), quantile(), median and quartiles
- 05Data frames in R: data.frame(...), rows = observations, columns = variables; head(), tail(), str(), unique(); column selection with $ and [ , ]
- 06Reading large/compressed files: read.table(gzfile('...csv.gz'), header=TRUE, sep=',')
- 07Formula notation and linear models: y~x; fit <- lm(y~x, data=...); summary(fit) for slope, intercept, Multiple R-squared; abline(fit)
- 08dplyr and factors: group_by/summarise/filter; factor(ifelse(...)) to derive a category (e.g. west/east)
Read R output: interpret an lm() fit and a grouped summary
- +1(a) The fitted line is y = 10 + 3*x (intercept + slope*x). The slope of about 3 means y increases by roughly 3 units for each 1-unit increase in x. A Multiple R-squared of about 0.9 means the model explains about 90% of the variation in y — a strong linear fit.
- +1(b) ifelse(Longitude > -100, 'west', 'east') labels each row 'west' when Longitude exceeds -100 and 'east' otherwise; factor(...) turns that character vector into a categorical variable (a factor) so it can be used to group. group_by(region) %>% summarise(mean(...)) then reports the mean for each region.
Key terms
- RStudio
- The standard IDE for R (including RStudio Cloud) used in FIT1043 for Week 8 and the R analysis stage of Assignment 2.
- Vector / c()
- R's basic data structure; c() concatenates values into a vector and operations are vectorised (applied element-wise), e.g. y <- x^2 + 10.
- data.frame
- R's tabular structure where each row is an observation and each column a variable; inspect with head(), str() and unique(), select columns with $ or [ , ].
- lm() and formula y~x
- Fits a linear model of y on x; summary(fit) reports the intercept, slope and Multiple R-squared, and abline(fit) draws the line of best fit.
- dplyr verbs
- group_by(), summarise() and filter() — R's chained grammar for grouped and filtered aggregation, analogous to pandas groupby/agg/filter.
- factor()
- Converts a character/numeric vector into a categorical variable with defined levels, so it can be used for grouping and category-based plots.
Introduction to R for Data Science FAQ
How is R different from Python for this unit?
They do the same jobs with different syntax. R uses <- for assignment, is heavily vectorised, and reaches for data.frame, lm() and dplyr where Python uses pandas and scipy. The unit teaches both because each is common in industry; the examinable skill is reading either fluently. Week 8 R is also the analysis language for Assignment 2.
How do I read the output of lm() and summary(fit)?
The fitted line is y = intercept + slope*x. The slope is the change in y per unit change in x, the intercept is y where x = 0, and the Multiple R-squared is the proportion of variance in y that the model explains (closer to 1 is a stronger fit). It is the R analogue of reading slope, intercept and r from scipy's linregress.
What does factor() do and why use it?
factor() turns a vector into a categorical variable with fixed levels. You use it whenever a column should be treated as categories rather than raw text or numbers — for example after factor(ifelse(Longitude > -100, 'west', 'east')) you can group_by that region and summarise a mean per group, or draw a boxplot split by category.
How do I read a large compressed file in R?
Use read.table with gzfile, e.g. read.table(gzfile('data.csv.gz'), header = TRUE, sep = ','), which streams a gzipped CSV without unzipping it first. Set your working directory with setwd() so the path resolves. This is exactly the kind of large-file read Assignment 2 involves after the BASH pre-processing stage.
Can AI help me learn R for FIT1043?
Yes. Sia can explain R syntax, read an lm() summary or a dplyr chain with you, and map pandas idioms to their R equivalents, step by step, then set fresh practice. It explains the method and checks your reasoning; it does not complete Assignment 2 for you, and Monash academic-integrity rules apply. Confirm details on Moodle.
Exam move
Learn R by mapping it onto what you already know from Python: assignment (<- vs =), vectors (c() vs a list/Series), data.frame vs DataFrame, lm() vs linregress, and dplyr's group_by/summarise/filter vs pandas groupby/agg/filter. Rehearse reading R output — write the fitted line from an lm() summary and interpret slope and R-squared — because code-reading in R is examinable just like pandas. Practise on the Week 8 ozone lab (read a gzipped file, fit a line, group by a factor and compare means) since that same pattern drives the R stage of Assignment 2. Keep a two-column Python-to-R cheat sheet for quick revision.
Working through Introduction to R for Data Science in FIT1043? Sia is AskSia’s AI Information Technology tutor — ask any FIT1043 Introduction to R for Data Science question and get a clear, step-by-step explanation grounded in how FIT1043 is taught and assessed. Read this chapter free, then take your hardest questions to Sia.