0
0
R Programmingprogramming~10 mins

mutate() for new columns in R Programming - Interactive Code Practice

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

Complete the code to add a new column 'total' as the sum of 'x' and 'y'.

R Programming
library(dplyr)
data <- tibble(x = 1:3, y = 4:6)
data <- data %>% mutate(total = x [1] y)
Drag options to blanks, or click blank then click option'
A/
B-
C*
D+
Attempts:
3 left
💡 Hint
Common Mistakes
Using subtraction or multiplication instead of addition.
Forgetting to use mutate() to add a new column.
2fill in blank
medium

Complete the code to create a new column 'ratio' as 'x' divided by 'y'.

R Programming
library(dplyr)
data <- tibble(x = 10:12, y = 2:4)
data <- data %>% mutate(ratio = x [1] y)
Drag options to blanks, or click blank then click option'
A/
B+
C*
D-
Attempts:
3 left
💡 Hint
Common Mistakes
Using multiplication or addition instead of division.
Mixing up the order of operands.
3fill in blank
hard

Fix the error in the code to correctly add a new column 'diff' as the difference between 'a' and 'b'.

R Programming
library(dplyr)
data <- tibble(a = 5:7, b = 3:5)
data <- data %>% mutate(diff = a [1] b)
Drag options to blanks, or click blank then click option'
A-
B+
C*
D/
Attempts:
3 left
💡 Hint
Common Mistakes
Using addition or multiplication instead of subtraction.
Swapping the order of operands.
4fill in blank
hard

Fill both blanks to create a new column 'score' as the product of 'm' and 'n' plus 10.

R Programming
library(dplyr)
data <- tibble(m = 2:4, n = 5:7)
data <- data %>% mutate(score = m [1] n [2] 10)
Drag options to blanks, or click blank then click option'
A*
B+
C-
D/
Attempts:
3 left
💡 Hint
Common Mistakes
Using addition before multiplication without parentheses.
Using subtraction or division instead of addition.
5fill in blank
hard

Fill all three blanks to create a new column 'result' as the uppercase of 'name', length of 'name', and check if length is greater than 4.

R Programming
library(dplyr)
data <- tibble(name = c('Anna', 'Brian', 'Clara'))
data <- data %>% mutate(result = list([1], [2], [3]))
Drag options to blanks, or click blank then click option'
Atoupper(name)
Bnchar(name)
Cnchar(name) > 4
Dtolower(name)
Attempts:
3 left
💡 Hint
Common Mistakes
Using tolower() instead of toupper().
Forgetting to compare length with a number.