Data 101 R Syntax

0.0(0)
studied byStudied by 0 people
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
Card Sorting

1/49

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

50 Terms

1
New cards

Q: How do you filter rows from students where GPA is above 3.5 using subset()?

A: subset(students, GPA > 3.5)

2
New cards

Q: How do you count how many students have a score over 79 using nrow()?

A: nrow(subset(students, score > 79))

3
New cards

Q: How do you create a frequency table for students’ seniority?

A: table(students$seniority)

4
New cards

Q: How do you filter students with GPA > 3.5 and seniority is "Freshman"?

A: subset(students, GPA > 3.5 & seniority == "Freshman")

5
New cards

Q: How do you calculate average GPA for each Seniority level?

A: tapply(students$GPA, students$seniority, mean)

6
New cards

Q: How do you count how many students are in each major?

A: tapply(students$major, students$major, length)

7
New cards

Q: How do you draw a boxplot comparing GPA by gender?

A: boxplot(GPA ~ gender, data = students)

8
New cards

Q: How do you draw a barplot of the number of students per major?

A: barplot(table(students$major))

9
New cards

Q: How do you make a scatter plot of score vs GPA?

A: plot(students$score, students$GPA)

10
New cards

Q: How do you fix this line if it's invalid: boxplot(score ~ seniority + major, data = moody)?

A: Convert one combined factor: boxplot(score ~ interaction(seniority, major), data = moody)

11
New cards

Q: How do you create a new column decision initialized to "No"?

A: decision <- rep("No", nrow(students))

12
New cards

Q: How do you update decision to "Yes" for CS majors with GPA > 3.5?

A: decision[students$major == "CS" & students$GPA > 3.5] <- "Yes"

13
New cards

Q: How do you classify students as "Pass" if score > 50, else "Fail"?

A: decision <- ifelse(students$score > 50, "Pass", "Fail")

14
New cards

Q: How do you assign scholarship levels: "Full" for GPA > 3.8, "Basic" for GPA > 3.0, else "None"?

decision <- rep("None", nrow(students))

decision[students$GPA > 3.0] <- "Basic"

decision[students$GPA > 3.8] <- "Full"

15
New cards

Q: How do you assign "Likely Graduate" if GPA > 3.2 OR Credits > 100?

A: decision[students$GPA > 3.2 | students$Credits > 100] <- "Likely Graduate"

16
New cards

Q: How do you build a 2-way frequency table for gender and department?

A: table(students$gender, students$department)

17
New cards

Q: How do you test for independence using chisq.test()?

A: chisq.test(table(students$gender, students$department))

18
New cards

Q: What happens with chisq.test(students$GPA, students$major)? Why?

A: It gives an error because both variables must be categorical. GPA is numeric.

19
New cards

Q: What is a false positive in Bayesian reasoning?

A: When the test says positive but the condition is actually false.

20
New cards

Q: How do you read and save the Moody dataset from a URL?

21
New cards

Q: What's the error here: tapply(data$score, data$grade)?

A: Missing third argument (function)

22
New cards

Q: What's the error here: subset(df, score > 80, grade)?

A: No error

23
New cards

Q: What's the error here: table(df$Gender, df$Income)?

A: No error

24
New cards

Q: Error in boxplot(height ~ gender + age, data = people)?

A: Nothing is wrong

25
New cards

Q: Will barplot(table(data$color)) fail?

A: No, it's valid

26
New cards

Q: What's the issue with nrow(data == "USA")?

A: nrow() requires a data frame

27
New cards

Q: Problem with plot(df$income, df$gender)?

A: Gender is not numeric

28
New cards

Q: Why does subset(df, age > 30 & salary) fail?

A: Salary must be logical

29
New cards

Q: Error in chisq.test(df$income, df$age)?

A: Variables must be categorical

30
New cards

Q: What's wrong with tapply(df$score, mean, df$group)?

A: Wrong order of arguments

31
New cards

Q: Best way to count rows where age > 30?

A: nrow(subset(people, age > 30))

32
New cards

Q: What does tapply(df$score, df$group, mean) compute?

A: Mean of score per group

33
New cards

Q: Best plot to compare score distribution by major?

Boxplot

34
New cards

Q: What does a barplot usually show?

A: Frequency of categories

35
New cards

Q: What's a false positive in Bayesian terms?

A: Test is positive but condition is false

36
New cards

Q: What is a true positive?

A: Test detects the condition correctly

37
New cards

Q: Purpose of chisq.test(table(df$Gender, df$Outcome))?

A: Test independence between gender and outcome

38
New cards

Q: Which function counts how many times each value appears in a column?

A: table()

39
New cards

Q: Best plot to show relationship between height and weight?

A: Scatter plot

40
New cards

Q: How to count number of females in dataset?

A: nrow(subset(students, gender == "F"))

41
New cards

Q: How to initialize prediction to "No" for all rows?

A: decision <- rep("No", nrow(df))

42
New cards

Q: Update decision to "Yes" for CS majors with GPA > 3.5?

A: decision[Major == "CS" & GPA > 3.5] <- "Yes"

43
New cards

Q: Classify students as "Pass" if score > 50, else "Fail"?

A: decision <- ifelse(df$Score > 50, "Pass", "Fail")

44
New cards

Q: Set "Excellent" for students with Attendance > 90%?

A: decision[df$Attendance > 90] <- "Excellent"

45
New cards

Q: Rule: GPA > 3.2 and Score > 80 → "Offer". Logic?

A: decision[GPA > 3.2 & Score > 80] <- "Offer"

46
New cards

Q: Scholarship logic with GPA > 3.0 = Basic, GPA > 3.8 = Full. What’s the result?

A: Students with GPA > 3.8 get "Full" (correct, order matters)

47
New cards

Q: Income category logic: Low, Medium, High. What’s the correct order to assign?

A: Set "Low", then "Medium", then "High" (to avoid overwriting)

48
New cards

Q: Logic for "Likely Graduate" if GPA > 3.2 OR Credits > 100?

A: decision[GPA > 3.2 | Credits > 100] <- "Likely Graduate"

49
New cards

Q: Logic to assign "Eligible" if Score > 85 AND GPA > 3.7?

A: decision[df$Score > 85 & df$GPA > 3.7] <- "Eligible"

50
New cards

Q: If GPA = 3.9, Score = 60, and code assigns "Maybe" for GPA > 3.5, "Yes" for Score > 80 — what’s the result?

A: "Maybe"