Skip to content

Programming Assignment 1: Quiz:R Programming(Data Science Specialization):Answers2025

Question 1

pollutantmean("specdata", "sulfate", 1:10)

4.064
❌ 6.026
❌ 3.782
❌ 3.666
❌ 4.868
❌ 6.545

Explanation:
The function computes the mean of all valid sulfate observations across monitors 1–10. After excluding NAs, the rounded mean = 4.064.


Question 2

pollutantmean("specdata", "nitrate", 70:72)

1.706
❌ 2.604
❌ 1.182
❌ 0.914
❌ 2.394
❌ 2.752

Explanation:
Average nitrate levels among monitors 70–72 (after removing missing values) give a mean ≈ 1.706.


Question 3

pollutantmean("specdata", "sulfate", 34)

1.477
❌ 1.573
❌ 1.300
❌ 0.450
❌ 0.680
❌ 0.591

Explanation:
The mean sulfate concentration for monitor 34 is 1.477 after NA removal.


Question 4

pollutantmean("specdata", "nitrate")

1.703
❌ 2.493
❌ 2.233
❌ 1.842
❌ 1.774
❌ 2.363

Explanation:
Across all 332 monitors, the average nitrate concentration (ignoring missing data) ≈ 1.703.


Question 5

cc <- complete("specdata", c(6, 10, 20, 34, 100, 200, 310))
print(cc$nobs)

228 148 124 165 104 460 232
❌ Other options

Explanation:
The complete() function counts completely observed cases per monitor ID. For the listed IDs, the counts are 228, 148, 124, 165, 104, 460, 232.


Question 6

cc <- complete("specdata", 54)
print(cc$nobs)

219
❌ 220
❌ 248
❌ 205
❌ 228
❌ 213

Explanation:
Monitor ID 54 has 219 complete cases.


Question 7

RNGversion("3.5.1")
set.seed(42)
cc <- complete("specdata", 332:1)
use <- sample(332, 10)
print(cc[use, "nobs"])

711 135 74 445 178 73 49 0 687 237
❌ Other options

Explanation:
Sampling is reproducible with the set seed. The 10 randomly selected nobs values match this sequence.


Question 8

cr <- corr("specdata")
cr <- sort(cr)
RNGversion("3.5.1")
set.seed(868)
out <- round(cr[sample(length(cr), 5)], 4)
print(out)

0.2688 0.1127 -0.0085 0.4586 0.0447
❌ Other options

Explanation:
Using seed 868 ensures reproducible random sampling from sorted correlations; the rounded result matches above.


Question 9

cr <- corr("specdata", 129)
cr <- sort(cr)
n <- length(cr)
RNGversion("3.5.1")
set.seed(197)
out <- c(n, round(cr[sample(n, 5)], 4))
print(out)

247.0000 0.1958 0.9304 -0.4851 -0.8229 -0.0679
❌ Other options

Explanation:
When threshold = 129, 247 valid monitor correlations exist. Sampled 5 correlation values match this output.


Question 10

cr <- corr("specdata", 2000)
n <- length(cr)
cr <- corr("specdata", 1000)
cr <- sort(cr)
print(c(n, round(cr, 4)))

3.0000 0.5342 -0.6713 0.3684
❌ Other options

Explanation:
Only 3 valid correlations exist with threshold 2000; then for threshold 1000, the sorted correlations are 0.5342, -0.6713, 0.3684.


🧾 Summary Table

Q# ✅ Correct Answer Key Concept
1 4.064 Mean sulfate (IDs 1–10)
2 1.706 Mean nitrate (IDs 70–72)
3 1.477 Mean sulfate (ID 34)
4 1.703 Mean nitrate (all monitors)
5 228 148 124 165 104 460 232 Complete cases
6 219 Complete cases for ID 54
7 711 135 74 445 178 73 49 0 687 237 Random sample reproducibility
8 0.2688 0.1127 -0.0085 0.4586 0.0447 Random correlations
9 247.0000 0.1958 0.9304 -0.4851 -0.8229 -0.0679 Threshold 129 correlations
10 3.0000 0.5342 -0.6713 0.3684 Threshold 1000 correlations