Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I want to extract the fith column of the data frame in R Studio. However, when I

ID: 2246497 • Letter: I

Question

I want to extract the fith column of the data frame in R Studio. However, when I am trying to do that with my code that I have displayed, I get an error saying "incorrect number of dimensions". When I test if the data is a data frame it says false. What am I doing wrong?

Code:

age <- c(23, 22, 28, 19, 36, 25, 22, 30, 34)
class <- c(1, 1, 1, 2, 1, 2, 2, 2, 1)
sex <- c(1, 2, 2, 2, 1, 1, 1, 2, 1)

comb <- data.frame(age, class, sex)
comb

boxplot <- ggplot(comb, aes(x = factor(class), y = age, col = factor(class), fill = factor(class))) +
geom_boxplot(alpha = 0.5) + facet_grid(.~sex)
boxplot

data.boxplot <- ggplot_build(boxplot)$data
str(data.boxplot)

data.boxplot[, 5]
data.boxplot$middle

is.data.frame(data.boxplot)

Explanation / Answer

Hey buddy,

I am not sure what you are trying to compute here but I can tell you about your errors that you have mentioned. Hope that helps!!

Error 1: The first error that you are getting as "incorrect no of dimensions" because data.boxplot[,5] is not a data frame its a list of only one element (replace this line with data.boxplot[1] to view its content)

Error 2: The second error you talked about because the data data.boxplot is not a data frame its a list. Check it by replacing with is.list(data.boxplot).

Note: Also if you still are unable to get desired output , kindly repost the question with sample output for better assistance.

Please feel free to reach out for further clarification.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote