I have very basic question about replacing the values in the groups most frequent value.
Here is what I mean
df <- data.frame(x=c(12,12,5,13,13,5),y=c(10,22,22,22,10,22),gr=gl(2,3))
> df
x y gr
1 12 10 1
2 12 22 1
3 5 22 1
4 13 22 2
5 13 10 2
6 5 22 2
As we can see for gr1
x column's frequent value is 12
and less frequent one is 5
and for y
column it is 10
. I would like to replace those values with lets say 666
and 777
for x and y ,respectively. In my real data the frequent x values are always the same but less frequent ones can change so general solutions would be good. dplyr
solution is preferable since the real data is already in the pipeline.
library(dplyr)
df%>%
group_by(gr)%>%
....
the expected output
> df
x y gr
1 12 777 1
2 12 22 1
3 666 22 1
4 13 22 2
5 13 777 2
6 666 22 2