Home > Software engineering >  R: Converting "tables" to "data frame: 'height' must be a vector or a matri
R: Converting "tables" to "data frame: 'height' must be a vector or a matri

Time:10-10

I found the following code in R that plots a histogram and colors some of the bars red:

n = floor(rnorm(10000, 500, 100))
t = table(n)

# get color counts
r <- floor(length(t)/2)
b <- length(t) - r

# repeat red r times, repeat black b times
barplot(t, col = rep(c("red", "black"), c(r, b)), border = NA)

# use "topleft" to place legend
legend("topleft", legend=c("group a", "group b"),
       col=c("red", "black"), lty = 1, cex=0.8)
title(main = "some title", 
      sub = "some title")

enter image description here

As seen in the above code, this requires that the data be in the "table" format.

Question: I am trying to make the same graph, but use "data frames" instead. This will allow me more flexibility (e.g. assign colors randomly to bars in the histogram) as I am more familiar with data frames. I tried to write the following code:

#randomly assign colors to different bars in the histogram and convert table into data frame

color <- c("red", "black")     
color_1 <- as.factor(sample(color, 10000, replace=TRUE, prob=c(0.5, 0.5)))
t = data.frame(t)
t$color_1 <-color_1
barplot(t, color = t$color_1)

But this returns several errors:

Error in `$<-.data.frame`(`*tmp*`, color_1, value = c(1L, 1L, 1L, 2L,  : 
  replacement has 10000 rows, data has 574

For some reason, the data has 583 rows instead of the 10,000 originally specified in (where did all the rows go!?) n = floor(rnorm(10000, 500, 100)) command.

Ignoring this, if I do assume 583 rows:

color <- c("red", "black")     
color_1 <- as.factor(sample(color, 574, replace=TRUE, prob=c(0.5, 0.5)))
t = data.frame(t)
t$color_1 <-color_1
barplot(t, color = t$color_1)

I get a new error:

Error in barplot.default(t, color = t$color_1) : 
  'height' must be a vector or a matrix

Can someone please show me how to fix this error?

Thanks

CodePudding user response:

Use the formula method for data.frame

t <- as.data.frame(t)
color <- c("red", "black")     
color_1 <- sample(color, nrow(t), replace=TRUE, prob=c(0.5, 0.5))
barplot(Freq ~ n, data = t, col = color_1, border = NA)

-output

enter image description here

  • Related