Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
322 views
in Technique[技术] by (71.8m points)

r - Create boxplots from separate data frames with different number of rows

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)
question from:https://stackoverflow.com/questions/66052011/create-boxplots-from-separate-data-frames-with-different-number-of-rows

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...