NEW
Font size
WorksheetsR Programming MCQs
Total questions: 16
Worksheet time: 16mins
Which of the following is a valid way to assign a value to a variable in R?
x := 5
x <- 5
x =: 5
5 == x
Which function is used to get a quick overview of a data frame structure in R?
details()
str()
analyze()
None
What does the select() function do?
Removes rows based on a condition
Modifies values in a column
Selects specific columns from a data frame
Sorts rows
Which function is used to reorder rows based on column values?
filter()
arrange()
group_by()
mutate()
What does summarise(mean_age = mean(age)) return?
The original dataset
A new dataset with one row and the average age
A plot
All ages greater than the mean
What is wrong with this line of code? data %>% filter(age > 30) %>% select[name]
Wrong data type
select uses [] instead of ()
Missing pipe
filter should come after select
What is the output of this code? data %>% filter(gender == 'F') %>% summarise(avg = mean(salary))
All female salaries
One row with average salary of females
Error
A histogram
Which function is used to read a CSV file into R?
read.csv()
load.csv()
open.csv()
write.csv()
How do you write a data frame df to a CSV file named data.csv?
export_csv(df, 'data.csv')
write.csv(df, 'data.csv')
write_csv(df, 'data.csv')
save.csv(df, file = 'data.csv')
Find the error: summarise(data, mean_salary = mean[salary])
Missing data
mean[salary] should be mean(salary)
Wrong function
Brackets are fine
Which of these will cause an error?
filter(data, salary > 50000)
select(data salary)
arrange(data, age)
mutate(data, income = salary * 1.1)
What's wrong with this code? data %>% group_by(department) %>% summarise(avg = average(salary))
group_by cannot be used here
average() should be mean()
Pipe not needed
No error
Which of the following lines contains a syntax error?
df %>% arrange(desc(salary))
df %>% select(name, age)
df %>% filter age > 25
df %>% mutate(bonus = salary * 0.1)
Which of the following is the correct syntax for a for loop in R?
for(i in 1:10) { print(i) }
for i in 1 to 10: print(i)
foreach i in 1:10 { print(i) }
None
Which function is used to loop through each element in a vector in R?
repeat()
apply()
None
for()
What will this code print? x <- 3 if (x > 5) { print('Big') } else { print('Small') }
Big
Nothing
Small
Error
