histogram

R code to categorize age into group/ bins/ breaks

僤鯓⒐⒋嵵緔 提交于 2019-12-17 06:11:56
问题 I am trying to categorize age into group so it will not be continuous. I have this code: data$agegrp(data$age>=40 & data$age<=49) <- 3 data$agegrp(data$age>=30 & data$age<=39) <- 2 data$agegrp(data$age>=20 & data$age<=29) <- 1 the above code is not working under survival package. It's giving me: invalid function in complex assignment Can you point me where the error is? data is the dataframe I am using. 回答1: I would use findInterval() here: First, make up some sample data set.seed(1) ages <-

Bin size in Matplotlib (Histogram)

北战南征 提交于 2019-12-17 03:24:15
问题 I'm using matplotlib to make a histogram. Is there any way to manually set the size of the bins as opposed to the number of bins? 回答1: Actually, it's quite easy: instead of the number of bins you can give a list with the bin boundaries. They can be unequally distributed, too: plt.hist(data, bins=[0, 10, 20, 30, 40, 50, 100]) If you just want them equally distributed, you can simply use range: plt.hist(data, bins=range(min(data), max(data) + binwidth, binwidth)) Added to original answer The

Overlay histogram with density curve

坚强是说给别人听的谎言 提交于 2019-12-17 02:36:28
问题 I am trying to make a histogram of density values and overlay that with the curve of a density function (not the density estimate). Using a simple standard normal example, here is some data: x <- rnorm(1000) I can do: q <- qplot( x, geom="histogram") q + stat_function( fun = dnorm ) but this gives the scale of the histogram in frequencies and not densities. with ..density.. I can get the proper scale on the histogram: q <- qplot( x,..density.., geom="histogram") q But now this gives an error:

Overlay normal curve to histogram in R

独自空忆成欢 提交于 2019-12-17 01:47:06
问题 I have managed to find online how to overlay a normal curve to a histogram in R, but I would like to retain the normal "frequency" y-axis of a histogram. See two code segments below, and notice how in the second, the y-axis is replaced with "density". How can I keep that y-axis as "frequency", as it is in the first plot. AS A BONUS: I'd like to mark the SD regions (up to 3 SD) on the density curve as well. How can I do this? I tried abline , but the line extends to the top of the graph and

How to flip the definition of edges(1) and edges(end) for the histc function in MATLAB?

孤者浪人 提交于 2019-12-14 03:59:44
问题 In MATLAB: n = histc(x,edges); is defined to behave as follows: n(k) counts the value x(i) if edges(k) <= x(i) < edges(k+1). The last bin counts any values of x that match edges(end). Is there any way to flip the end behavior such that n(1) counts any values of x that match edges(1), and n(end) counts the values x(i) that satisfy edges(end-1) <= x(i) < edges(end)? 回答1: Consider the following code: n = histc(x, [edges(1) edges]); n(1) = sum(x==edges(1)); n(end) = []; According to the question

Combined frequency histogram using two attributes

假如想象 提交于 2019-12-14 03:54:27
问题 I'm using ggplot2 to create histograms for two different parameters. My current approach is attached at the end of my question (including a dataset, which can be used and loaded right from pasetbin.com), which creates a histrogram visualizing the frequency for the spatial distribution of logged user data based on the "location"-attribute (either "WITHIN" or "NOT_WITHIN"). a histogram visualizing the frequency for the distribution of logged user data based on the "context"-attribute (either

A histogram with a bar for each frequency value

风格不统一 提交于 2019-12-14 03:08:47
问题 I have a data of 100 rolls of two dice, which can take on 11 values -> {2,3,4,5,6,7,8,9,10,11,12} How do I create a histogram in R that would show all 11 of them, each as it's own bar with a label for each one of them. hist(data$X1,breaks=c(1,2,3,4,5,6,7,8,9,10,11,12,13),col = "lightblue",xlab="Sum of a roll") Only gives 10 bars. EDIT: I did something approximate with moving breaks 0.5 up like so: breaks=c(1.5,2.5,3.5,4.5,5.5,6.5,7.5,8.5,9.5,10.5,11.5,12.5,13.5) 回答1: Histograms can also be

How to implement a part of histogram equalization in matlab without using for loops and influencing speed and performance

三世轮回 提交于 2019-12-13 23:26:27
问题 Suppose that I have these Three variables in matlab Variables I want to extract diverse values in NewGrayLevels and sum rows of OldHistogram that are in the same rows as one diverse value is. For example you see in NewGrayLevels that the six first rows are equal to zero. It means that 0 in the NewGrayLevels has taken its value from (0 1 2 3 4 5) of OldGrayLevels. So the corresponding rows in OldHistogram should be summed. So 0+2+12+38+113+163=328 would be the frequency of the gray level 0 in

How do I scale the y-axis on a histogram by the x values in R?

淺唱寂寞╮ 提交于 2019-12-13 16:02:08
问题 I have some data which represents a sizes of particles. I want to plot the frequency of each binned-size of particles as a histogram, but scale the frequency but the size of the particle (so it represents total mass at that size.) I can plot a histogram fine, but I am unsure how to scale the Y-axis by the X-value of each bin. e.g. if I have 10 particles in the 40-60 bin, I want the Y-axis value to be 10*50=500. 回答1: You would better use barplot in order to represent the total mass by the area

opencv calcHist results are not what expected

为君一笑 提交于 2019-12-13 15:47:01
问题 In openCV, I have a matrix of integers (a 4000x1 Mat). Each time I read different ranges of this matrix: Mat labelsForHist = labels(Range(from,to),Range(0,1)); The size of the ranges is variable. Then I convert the labelsForHist matrix to float(because calcHist doesnt accept int values!) by using: labelsForHist.convertTo(labelsForHistFloat, CV_32F); After this I call calcHist with these parameters: Mat hist; int histSize = 4000; float range[] = { 0, 4000 } ; int channels[] = {0}; const float*