rayray
rayray

Reputation: 35

How can I label my data points according to defined ranges?

I have a dataframe df and I set interval points which are saved in a vector pts. Now I want to label my data into these intervals. I tried using the cut() function, but I always get the mistake, that x is not numeric, even though I converted it to numeric.

My dataframe df

        date    amount
1    2012-07-01 2.3498695
2    2012-08-01 0.6984866
3    2012-09-01 0.9079118
4    2012-10-01 2.8858218
5    2012-11-01 1.2406948
6    2012-12-01 2.3140496
7    2013-01-01 1.5904573
8    2013-02-01 3.2531825
9    2013-03-01 4.2962963
10   2013-04-01 3.3287101
11   2013-05-01 3.7698413
12   2013-06-01 1.4376997
13   2013-07-01 5.0687285
14   2013-08-01 4.4520548
15   2013-09-01 5.5063913
16   2013-10-01 5.5676856
17   2013-11-01 6.2686567
18   2013-12-01 11.021069

My vector pts with column Min with interval points

pts$Min
[1]  3  6 11

My new dataframe should look like this:

    date     amount      IntervalRange
1    2012-07-01 2.3498695    1
2    2012-08-01 0.6984866    1
3    2012-09-01 0.9079118    1
4    2012-10-01 2.8858218    2
5    2012-11-01 1.2406948    2
6    2012-12-01 2.3140496    2
7    2013-01-01 1.5904573    3
8    2013-02-01 3.2531825    3
9    2013-03-01 4.2962963    3
10   2013-04-01 3.3287101    3
11   2013-05-01 3.7698413    3
12   2013-06-01 1.4376997    4
13   2013-07-01 5.0687285    4
14   2013-08-01 4.4520548    4
15   2013-09-01 5.5063913    4
16   2013-10-01 5.5676856    4
17   2013-11-01 6.2686567    4
18   2013-12-01 11.021069    4

SO, I tried this:

df_cut <- data.frame(as.numeric(df$date), "IntervalRange" = cut(
                                                df,
                                                breaks=pts$Min))

Which results in this error message:

Error in cut.default(df, breaks = pts$Min) : 'x' must be numeric

My questions now are:

  1. Why do I get this error message? I already changed it to numeric...
  2. Can I achieve my desired output by using the cut() and findIntervals() functions also when using other datasets with other interval points?

Upvotes: 0

Views: 129

Answers (1)

zzabaa
zzabaa

Reputation: 86

You are lacking the value (or the column) in the cut function. Your command should be

data.frame(as.numeric(df$date), "IntervalRange" = cut(df$amount,  breaks=pts$Min))

Hope this helps!

Upvotes: 1

Related Questions