Reputation: 18800
I am new to R, I am trying to introduce a group column based on the data in a column.
Example of the data.frame
1 11.3178501 4 9 11.618880
2 10.3969713 20 8 11.047486
8 9.5067421 14 7 10.079806
6 6.6135932 6 6 7.002669
4 5.4157174 2 5 5.566232
17 3.8860793 5 4 4.235564
16 3.8183699 15 3 4.406416
11 1.2574765 18 2 1.885113
15 0.7084411 7 1 1.130990
First column is the index introduced by the R but I sorted so the order is different, what I am trying to do is introduce a column that defines the bracket that each row is belong to based on the last column value.
so if last column value is between 0-5 => 1, 5-0 => 2
etc then we add a new column at the end group -> 1,2,3...
16 3.8183699 15 3 4.406416 1
11 1.2574765 18 2 1.885113 2
15 0.7084411 7 1 1.130990 2
I tried the following dataFrame$column4 < 5
but this gave me a boolean value so I thought I'll multiply that by 1 then i got the following
0 0 0 0 0 1 1 1 1
I am not sure if I am on the right track.
Upvotes: 1
Views: 2234
Reputation: 18628
Justin's answer is great; yet if you want to implement dumber cut on your own, you can do this this way. First, you define a vector with your thresholds, like thre<-c(0,5,10,15)
, then do an outer comparison of your values and those thresholds with greater-than operator and sum the rows of such created matrix like this:
rowSums(outer(values,thre,'>'))
And voila, all values in (0,5] are now 1, (5,10] are 2, etc.
Wrapped in a function, it could look like this:
ultraDumbCut<-function(v,thre) rowSums(outer(v,thre,'>'))
Made a bit more intelligent, like this:
dumbCut<-function(v,jump=5,thre=seq(0,max(v),by=jump)) rowSums(outer(v,thre,'>'))
so that dumbCut(1:7)
is 1 1 1 1 1 2 2
, dumbCut(1:7,3)
is 1 1 1 2 2 2 3
and dumbCut(1:7,thre=c(0,2,3,5))
is 1 1 2 3 3 4 4
.
Next step is to convert the output to a factor (because using numbers for categories in R is simply a masochism) and generate a meaningful level names, so basically replicating actual cut
.
Upvotes: 1
Reputation: 43245
Even given your comment, I would still suggest cut
. It is in base R and usually not considered a fancy function.
df <- structure(list(V1 = c(1L, 2L, 8L, 6L, 4L, 17L, 16L, 11L, 15L),
V2 = c(11.3178501, 10.3969713, 9.5067421, 6.6135932, 5.4157174,
3.8860793, 3.8183699, 1.2574765, 0.7084411), V3 = c(4L, 20L,
14L, 6L, 2L, 5L, 15L, 18L, 7L), V4 = c(9L, 8L, 7L, 6L, 5L,
4L, 3L, 2L, 1L), V5 = c(11.61888, 11.047486, 10.079806, 7.002669,
5.566232, 4.235564, 4.406416, 1.885113, 1.13099)), .Names = c("V1",
"V2", "V3", "V4", "V5"), class = "data.frame", row.names = c(NA,
-9L))
df$groups <- cut(df$V5, seq(0,15, by=5))
> df
V1 V2 V3 V4 V5 groups
1 1 11.3178501 4 9 11.618880 (10,15]
2 2 10.3969713 20 8 11.047486 (10,15]
3 8 9.5067421 14 7 10.079806 (10,15]
4 6 6.6135932 6 6 7.002669 (5,10]
5 4 5.4157174 2 5 5.566232 (5,10]
6 17 3.8860793 5 4 4.235564 (0,5]
7 16 3.8183699 15 3 4.406416 (0,5]
8 11 1.2574765 18 2 1.885113 (0,5]
9 15 0.7084411 7 1 1.130990 (0,5]
>
Finally, if integers are what you want, you can coerce the groups
to integers using factor
.
df$groups <- as.integer(df$groups)
> as.integer(df$groups)
[1] 3 3 3 2 2 1 1 1 1
Upvotes: 5