I want to create a subset of data and gather the data based on their max value: Here is the code:
mat=matrix(c(0,0,0,1,2,3,4,5,0,0,0,0), ncol=1)
mat=as.data.frame(mat)
colnames(mat) <- sub("V1", "value", colnames(mat))
gr=matrix(c(1,2,2,2,2,3,4,4,4,5,5,5), ncol=1)
gr=as.data.frame(gr)
colnames(gr) <- sub("V1", "group", colnames(gr))
df=as.data.frame(cbind(mat, gr))
data = subset(df, value == max(value))
So I created a dataframe df
which looks like this:
value group
1 0 1
2 0 2
3 0 2
4 1 2
5 2 2
6 3 3
7 4 4
8 5 4
9 0 4
10 0 5
11 0 5
12 0 5
So I want to gather the data in a subset data frame based on the max value e.g.
- For group 1 the max value is 0.
- For group 2 max value is 2
- For group 3 max value is 3 and so on.
The result then should be:
value group
1 0 1
5 2 2
6 3 3
8 5 4
12 0 5
Instead with subset(df, value == max(value))
I get:
value group
8 5 4
Any suggestion of what function I could use to solve it?