I am trying to recode character values from a dataframe in R as:
freq(prueba$frutas, sort = "dec")
n % val%
BANANA 36793 61.9 66.3
UVA 5206 8.8 9.4
FRESA 3153 5.3 5.7
CEREZA 2883 4.9 5.2
MANZANA 2748 4.6 4.9
MANGO 1680 2.8 3.0
MELON 1063 1.8 1.9
SANDIA 1061 1.8 1.9
OTROS 766 1.3 1.4
PERA 97 0.2 0.2
KIWI 72 0.1 0.1
BROCOLI 1 0.0 0.0
NA 3877 6.5 NA
So I would like to replace all "character" values in the dataframe where frequency is lower than 6% for "other", so the result would be:
freq(prueba$frutas, sort = "dec")
n % val%
BANANA 36793 61.9 66.3
OTHER 13524 22.8 24.3
UVA 5206 8.8 9.4
NA 3877 6.5 NA
Usually I do the replacement of values using replace as show next, but I would like to know if there is a better way to do it using the 6% value.
prueba$frutas <- replace(prueba$frutas, which(prueba$frutas != c("BANANA","UVAS")) , "OTHER")