Skip to main content
cambio parte de la pregunta
Origen Enlace
Caro
  • 807
  • 2
  • 10
  • 24

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:

  1. 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. "ASÍ, ahora tanto RAM 4 y RAM 11 tendrían 2 posibles combinaciones distintas de Reb y mes". 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", "4", "6","7","8","9","11""10","10"),
freq = c("31", "68", "10", "31","31,"34","40","9","15","65","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?

  1. Volver a renumerar consecutivamente RAM, ya que 5 y 10 han desaparecido.

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:

  1. 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?

  1. Volver a renumerar consecutivamente RAM, ya que 5 y 10 han desaparecido.

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:

  1. 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. "ASÍ, ahora tanto RAM 4 y RAM 11 tendrían 2 posibles combinaciones distintas de Reb y mes". 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", "4", "6","7","8","9","10","10"),
freq = c("31", "68", "10", "31","31,"34","40","9","15","65","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?

título editado
Enlace
Patricio Moracho
  • 61.1k
  • 12
  • 42
  • 72

Cómo ¿Cómo comparo filas utilizando ifelse?

He introducido el código como tal, estaba sin formatear. Apertura de signos de interrogación
Origen Enlace

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")

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")

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")

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 ifelseifelse, 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 ifelseifelse y compara las filas. Cómo¿Cómo lo hago? Alguna¿Alguna otra idea?

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")

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?

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")
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?

Origen Enlace
Caro
  • 807
  • 2
  • 10
  • 24
Loading