Domanda

I have some code in R that produces a histogram in R about the frequency of different power(watts) values for a machine at different points in time.

The histogram is divided in "n" number of bins or cells.

Is there an easy way to calculate the mean value and the standard deviation IN EACH bin?

So for example,

bin 1, mean = 0.5, sd=0.01
bin 2, mean = 3.5, sd=0.23
bin 3, mean = 4.5, sd=0.35

Any ideas?

È stato utile?

Soluzione

Yeah. So let's say you have a vector or column of a data frame of the observations of the power of a machine, P.

P <- c(100,80,100,120,80)

So break it into bins however you want:

C <- cuts(P, breaks=3)

Break the break labels into "numbers"

> C <- as.numeric(C)
> C
[1] 2,1,3,2,1

Now you can get information by each break

by(P, C, mean)
by(P, C, sd)

Or all together:

by(P, C, function(x) c(mean(x), sd(x))

And more reader friendly:

by(P, C, function(x) paste(c("Mean : ", "SD : "), c(mean(x), sd(x)), sep=" | "))
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top