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.
我在R中有一些代码,它在R中产生一个关于机器在不同时间点的不同功率(瓦特)值的频率的直方图。
The histogram is divided in "n" number of bins or cells.
直方图被划分为“n”个区间或单元格。
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?
有任何想法吗?
1 个解决方案
#1
1
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。
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=" | "))
#1
1
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。
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=" | "))