0
0
R Programmingprogramming~10 mins

Factor in analysis and plotting 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 convert the variable 'group' into a factor.

R Programming
group <- c('A', 'B', 'A', 'C')
group_factor <- [1](group)
Drag options to blanks, or click blank then click option'
Aas.character
Bas.numeric
Cfactor
Dlist
Attempts:
3 left
💡 Hint
Common Mistakes
Using as.numeric() which converts to numbers, not categories.
Using as.character() which keeps it as text.
2fill in blank
medium

Complete the code to create a bar plot of the factor variable 'group_factor'.

R Programming
library(ggplot2)
data <- data.frame(group = group_factor)
ggplot(data, aes(x = [1])) + geom_bar()
Drag options to blanks, or click blank then click option'
Agroup
Baes
Cdata
Dgroup_factor
Attempts:
3 left
💡 Hint
Common Mistakes
Using the factor variable name directly instead of the data frame column name.
Passing the whole data frame instead of a column.
3fill in blank
hard

Fix the error in the code to reorder the factor levels by frequency.

R Programming
data$group <- factor(data$group, levels = names(sort(table(data$group), [1] = TRUE)))
Drag options to blanks, or click blank then click option'
Adecreasing
Bincreasing
Cdesc
Dorder
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'increasing' which is not a valid argument.
Using 'desc' or 'order' which are not arguments of sort().
4fill in blank
hard

Fill both blanks to create a summary table of counts and proportions for the factor 'group'.

R Programming
summary_table <- data.frame(
  count = table(data$group),
  proportion = prop.table(table(data$[1]))
)
rownames(summary_table) <- levels(data$[2])
Drag options to blanks, or click blank then click option'
Agroup
Bgroup_factor
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing 'group' and 'group_factor' which causes errors.
Using incorrect variable names.
5fill in blank
hard

Fill all three blanks to create a ggplot showing counts with reordered factor levels and custom colors.

R Programming
ggplot(data, aes(x = factor(data$[1], levels = names(sort(table(data$[2]), [3] = TRUE))))) +
  geom_bar(fill = 'skyblue') +
  xlab('Group') + ylab('Count') +
  ggtitle('Bar Plot with Reordered Factor Levels')
Drag options to blanks, or click blank then click option'
Agroup
Cdecreasing
Dincreasing
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names for blanks 1 and 2.
Setting 'decreasing' to FALSE or using 'increasing'.