Reputation: 7517
In my function below, from time to time (please run a few times to see), I get an error message from inside the data.frame
that says x
and y
differ by 2 rows.
I was wondering how this occasional error could be fixed?
x = rnorm(1e2)
h = hist(x = x, plot = F)
DF = data.frame(
x = unlist(sapply(1:length(h$mids), function(i) rep(h$mids[i], each = h$counts[i]))),
y = unlist(sapply(h$counts, function(c) 1:c)))
plot(DF$x, DF$y)
Error in data.frame(x = unlist(sapply(1:length(h$mids), function(i) rep(h$mids[i], :
arguments imply differing number of rows: 100, 102
Upvotes: 0
Views: 33
Reputation: 388982
You get some h$counts
as 0 and when you run unlist(sapply(h$counts, function(c) 1:c)))
it generates a sequence from 1:0
which is unwanted. You can modify the way you create the dataframe and it should work ok.
DF1 <- data.frame(x = rep(h$mids, h$counts),y = sequence(h$counts))
Upvotes: 1