Tengo el siguiente dataframe, donde "RAM" correponde a la combinación única de las variables "Reb", "camp" y "mes". freq es la frecuencia de cada grupo "RAM"
df1 <- data.frame(camp = c("2006", "2006", "2009", "2009","2009", "2010", "2010","2010","2010","2011","2011","2011"), Reb = c("310020000003", "310020000003", "310020000003", "310020000003", "310020000003", "310020000003", "310020000003","310020000003","310020000003","310020000003","310020000003"), mes = c("1", "2", "1", "2", "3", "12","1","2","3","11","12"), RAM = c("1", "2", "3", "4", "5", "6","7","8","9","10","11"), freq = c("31", "68", "10", "30", "1", "34","40","9","15","3","62")
Tengo que hacer 2 cosas:
- Reasignar aquellos "RAM" que tienen una frecuencia menor a 10 al RAM siguiente o anterior, siempre y cuando pertenezcan al mismo "camp". En este caso en concreto tendría que reasignar RAM 5 al RAM 4, ya que pertencen al mismo "camp" y el RAM 10 al 11. Hasta aquí el output sería:
df2 <- data.frame(camp = c("2006", "2006", "2009", "2009","2009", "2010", "2010","2010","2010","2011","2011","2011"), Reb = c("310020000003", "310020000003", "310020000003", "310020000003", "310020000003", "310020000003", "310020000003","310020000003","310020000003","310020000003","310020000003"), mes = c("1", "2", "1", "2", "3", "12","1","2","3","11","12"), RAM = c("1", "2", "3", "4", "6","7","8","9","11"), freq = c("31", "68", "10", "31","34","40","9","15","65")
Para esto he pensado usar ifelse, poniendo como condición que si la fila 1 es igual a la 2, entonces RAM +1... pero no sé como usar nrow para integrarla en el ifelse y compara las filas. Cómo lo hago? Alguna otra idea?
- Volver a renumerar consecutivamente RAM, ya que 5 y 10 han desaparecido.