1

I'm trying to make a data frame to then create a boxplot from. The data frame should contain 3 vectors of varying sizes. Let's say the data is currently in a$data, b$data and c$data, and are of lengths 7, 50, 200.

Here is a simplified version of my code, where the cbind step errors:

# create initial df
    df <- data.frame()
# set column names
    colnames(df) <- c("a", "b", "c")
# bind original data to new data frame: 
    df <- cbind(df, a$data, b$data, c$data)
# draw boxplot
    boxplot(df)
6
  • 2
    df <- data.frame creates a function exactly equal to function data.frame. In R, this is the same as x <- 1; y <- x. The objects x and y are now identical(). See what identical(df, data.frame) returns. Can you post the output of dput(head(a$data))? Commented Feb 4, 2021 at 18:59
  • 1
    Perhaps it's just your sample code, but df here would be a function, not a data object. (Perhaps I'm being too literal with your sample data.) Further, you cannot cbind frames of different numbers of rows ... okay, you sometimes can get away with it, but if it doesn't warn or error, you should be concerned (and more careful). Commented Feb 4, 2021 at 18:59
  • @RuiBarradas - sure, look like: c(7.8192479, 7.910274, 7.51289579, 7.189279, 7.5897185, 7.14218948) Commented Feb 4, 2021 at 19:00
  • Apologies, data.frame was indeed just an example, I'll edit it now. Commented Feb 4, 2021 at 19:04
  • 4
    A data.frame can't "contain [...] vectors of varying sizes". But lists can, e.g. l = list(x = rnorm(5, 2), y = rnorm(10, 3), z = rnorm(20, 1)). And boxplot happily eats lists: ; boxplot(l). Commented Feb 4, 2021 at 19:05

3 Answers 3

3

A data.frame can not "contain [...] vectors of varying sizes". But lists can, e.g.

l = list(x = rnorm(5, 2), y = rnorm(10, 3), z = rnorm(20, 1)).

And boxplot happily eats lists:

boxplot(l) enter image description here

Sign up to request clarification or add additional context in comments.

3 Comments

I am among today's 10,000 people. (Not that I use base R boxplots much these days, but ... how did I not know this?)
Do not despair @r2evans, this winter is the suitable time to find yourself in the Chesterfield armchair with your favorite Burgundy red and...The R Reference Index.
Yes, that manual is ... riveting ... action sequences unequaled by anything except perhaps Webster's Dictionary.
2

Try the following code and see if it solves the problem.
It creates a data set in the long format, with a column vector of variables "a", "b" and "c", and their respective values. Then plots the data with the formula interface.

variable <- rep(c("a", "b", "c"), 
                c(length(a$data),length(b$data), length(c$data)))
value <- c(a$data, b$data, c$data)

df <- data.frame(variable, value)
boxplot(value ~ variable, data = df)

Comments

2

I tend to think of groups of boxplots in a "long-data" sense.

Fake-data:

set.seed(2021)
df1 <- data.frame(x=runif(10)); df2 <- data.frame(x=runif(20)); df3 <- data.frame(x=runif(100))
head(df1,3); head(df2,3); head(df3,3)
#           x
# 1 0.4512674
# 2 0.7837798
# 3 0.7096822
#            x
# 1 0.02726706
# 2 0.83749040
# 3 0.60324073
#            x
# 1 0.03277595
# 2 0.94270937
# 3 0.94773844

Combine into one long frame:

# tidyverse
dfall <- dplyr::bind_rows(dplyr::lst(df1, df2, df3), .id = "id")

# data.table
dfall <- rbindlist(list(df1=df1, df2=df2, df3=df3), idcol = "id")

# base R
lst_of_frames <- list(df1=df1, df2=df2, df3=df3)
lst_of_frames <- Map(function(x,nm) transform(x, { id = nm }), lst_of_frames, names(lst_of_frames))
dfall <- do.call(rbind, lst_of_frames)

Plot:

boxplot(x ~ id, data = dfall)

simple boxploot

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.