Bird
Raised Fist0
R Programmingprogramming~10 mins

R vs Python for data analysis in R Programming - Interactive Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a vector of numbers from 1 to 5 in R.

R Programming
numbers <- [1](1, 2, 3, 4, 5)
Drag options to blanks, or click blank then click option'
Ac
Bmatrix
Cdata.frame
Dlist
Attempts:
3 left
💡 Hint
Common Mistakes
Using list() creates a list, not a numeric vector.
Using data.frame() creates a data frame, which is a table-like structure.
Using matrix() creates a matrix, which is two-dimensional.
2fill in blank
medium

Complete the code to calculate the mean of a numeric vector named 'data' in R.

R Programming
average <- [1](data)
Drag options to blanks, or click blank then click option'
Amean
Bsum
Cmedian
Dmode
Attempts:
3 left
💡 Hint
Common Mistakes
Using sum() returns the total, not the average.
Using median() returns the middle value, not the average.
Using mode() is not a base R function for numeric vectors.
3fill in blank
hard

Fix the error in the code to subset rows where the 'age' column is greater than 30 in a data frame 'df'.

R Programming
subset_df <- df[df$[1] > 30, ]
Drag options to blanks, or click blank then click option'
AAge
Bage
Cage()
Dage[]
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'Age' instead of 'age' if the column name is lowercase.
Using parentheses like 'age()' which is incorrect for column access.
Using brackets like 'age[]' which is not valid syntax here.
4fill in blank
hard

Fill both blanks to create a new data frame with only rows where 'score' is greater than 80 and select the 'name' and 'score' columns.

R Programming
high_scores <- df[df$[1] [2] 80, c('name', 'score')]
Drag options to blanks, or click blank then click option'
Ascore
B>
C<
D==
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' or '==' instead of '>' for filtering scores above 80.
Using wrong column names or misspelling 'score'.
5fill in blank
hard

Fill all three blanks to create a summary table with the mean of 'height' grouped by 'gender' using the dplyr package.

R Programming
library(dplyr)
summary <- df %>% group_by([1]) %>% summarise([2] = mean([3]))
Drag options to blanks, or click blank then click option'
Agender
Bavg_height
Cheight
Dscore
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong column names for grouping or summarising.
Not using the pipe operator %>% correctly.

Practice

(1/5)
1. Which language is known for having many built-in statistical functions and is often preferred by statisticians?
easy
A. R
B. Python
C. Java
D. SQL

Solution

  1. Step 1: Understand language focus

    R is designed with statistics and data analysis in mind, offering many built-in statistical tools.
  2. Step 2: Compare with Python

    Python is more general-purpose and flexible but requires additional libraries for statistics.
  3. Final Answer:

    R -> Option A
  4. Quick Check:

    Statistics focus = R [OK]
Hint: R has built-in stats tools; Python needs libraries [OK]
Common Mistakes:
  • Confusing Python's flexibility with built-in stats
  • Thinking Java or SQL are for stats analysis
2. Which of the following is the correct way to import the pandas library in Python for data analysis?
easy
A. library(pandas)
B. import pandas library
C. import pandas as pd
D. use pandas

Solution

  1. Step 1: Recall Python import syntax

    Python uses 'import' keyword and 'as' to create an alias.
  2. Step 2: Check each option

    import pandas as pd uses correct Python syntax. library(pandas) is R syntax. Options C and D are invalid.
  3. Final Answer:

    import pandas as pd -> Option C
  4. Quick Check:

    Python import syntax = import pandas as pd [OK]
Hint: Python imports use 'import ... as ...' [OK]
Common Mistakes:
  • Using R syntax in Python
  • Missing 'as' keyword
  • Wrong import keywords
3. Consider this R code snippet for data analysis:
data <- data.frame(x = 1:3, y = c(4, 5, 6))
summary(data)

What will be the output of summary(data)?
medium
A. An error because summary() is not a valid function
B. A summary of statistics (min, max, mean) for columns x and y
C. Prints the data frame as is
D. Only the first row of data

Solution

  1. Step 1: Understand summary() in R

    summary() gives basic statistics like min, max, mean for each column in a data frame.
  2. Step 2: Apply to given data

    Columns x and y have numeric values, so summary() will show stats for both.
  3. Final Answer:

    A summary of statistics (min, max, mean) for columns x and y -> Option B
  4. Quick Check:

    summary() in R = stats summary [OK]
Hint: summary() in R shows stats for each column [OK]
Common Mistakes:
  • Thinking summary() prints data
  • Assuming summary() causes error
  • Confusing with head() function
4. This Python code tries to create a DataFrame but has an error:
import pandas as pd
data = {'a': [1, 2], 'b': [3, 4, 5]}
df = pd.DataFrame(data)

What is the error and how to fix it?
medium
A. Missing import statement; add 'import numpy as np'
B. Syntax error in dictionary definition; fix quotes
C. No error; code runs fine
D. Error due to unequal list lengths; fix by making lists same length

Solution

  1. Step 1: Identify cause of error

    DataFrame columns must have equal length lists; here 'a' has 2 elements, 'b' has 3.
  2. Step 2: Fix the error

    Make both lists same length, e.g., 'b': [3, 4] or 'a': [1, 2, 3].
  3. Final Answer:

    Error due to unequal list lengths; fix by making lists same length -> Option D
  4. Quick Check:

    DataFrame columns equal length = fix error [OK]
Hint: DataFrame columns must have equal length lists [OK]
Common Mistakes:
  • Ignoring list length mismatch
  • Thinking import numpy needed
  • Assuming syntax error in dict
5. You want to automate a data cleaning task that involves reading multiple CSV files, merging them, and applying machine learning models. Which language is generally better suited for this task and why?
hard
A. Python, because it is flexible and has many libraries for automation and ML
B. R, because it has built-in machine learning and automation tools
C. R, because it is easier to write loops and automation scripts
D. Python, because it has better built-in statistical functions

Solution

  1. Step 1: Analyze task requirements

    Task needs automation, file handling, merging, and machine learning.
  2. Step 2: Compare language strengths

    Python is widely used for automation and machine learning with many libraries like pandas, scikit-learn.
  3. Step 3: Evaluate options

    R is strong in stats but less flexible for automation; Python fits better here.
  4. Final Answer:

    Python, because it is flexible and has many libraries for automation and ML -> Option A
  5. Quick Check:

    Automation + ML = Python [OK]
Hint: Python excels in automation and machine learning tasks [OK]
Common Mistakes:
  • Choosing R for automation
  • Confusing built-in stats with automation
  • Ignoring Python's library ecosystem
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard