structure(list(
fecha = c("Fuente:La Nueva Viga, DF", "20/02/2020",
"20/02/2020", "20/02/2020", "20/02/2020", "Fuente:Monterrey, Nuevo León",
"20/02/2020", "20/02/2020", "20/02/2020", "20/02/2020", "17/02/2020",
"17/02/2020"),
producto = c("Fuente:La Nueva Viga, DF", "Aleta de raya",
"Bandera", "Besugo", "Cazón con cabeza", "Fuente:Monterrey, Nuevo León",
"Huachinango Golfo", "Pampano", "Sargo", "Trucha marina", "Huachinango Golfo",
"Pampano"), origen = c("Fuente:La Nueva Viga, DF", "Tabasco",
"Campeche", "Veracruz", "Veracruz", "Fuente:Monterrey, Nuevo León",
"Tamaulipas", "Tamaulipas", "Tamaulipas", "Tamaulipas", "Tamaulipas",
"Tamaulipas"),
pmin = c("Fuente:La Nueva Viga, DF", "23.00",
"35.00", "15.00", "60.00", "Fuente:Monterrey, Nuevo León", "165.00",
"--", "--", "--", "210.00", "--"), pmax = c("Fuente:La Nueva Viga, DF",
"27.00", "39.00", "19.00", "65.00", "Fuente:Monterrey, Nuevo León",
"200.00", "--", "--", "--", "220.00", "--"),
pfrec = c("Fuente:La Nueva Viga, DF",
"25.00", "37.00", "17.00", "63.00", "Fuente:Monterrey, Nuevo León",
"190.00", "195.00", "84.00", "98.00", "215.00", "195.00"),
obs = c("Fuente:La Nueva Viga, DF",
"", "", "", "", "Fuente:Monterrey, Nuevo León", "OBS", "OBS",
"OBS", "OBS", "OBS", "OBS"),
category = c("pescado", "pescado",
"pescado", "pescado", "pescado", "pescado", "pescado", "pescado",
"pescado", "pescado", "pescado", "pescado")),
row.names = c(2L, 3L, 4L, 5L, 6L, 341L, 342L, 343L, 344L, 345L, 346L, 347L), class = "data.frame")
The dataset above has 6 columns, but the table comes with a sub-header (e.g., Fuente: La Nueva Viga, DF).
The full dataset is much larger (> 9000 rows); there are a different number of rows under each sub-header.
I would like to transpose the sub-headers to create a new column called "Fuente" that displays the text after the ":".
Due to the number of rows in the data.frame and inconsistency of the number of columns between each sub-header, I can't easily use rep() or something similar (or at least I can't figure out how).
An example of the output that I am looking for would be this:
fecha producto origen pmin pmax pfrec obs category Fuente
1 20/02/2020 Aleta de raya Tabasco 23.00 27.00 25.00 pescado La Nueva Viga, DF
2 20/02/2020 Bandera Campeche 35.00 39.00 37.00 pescado La Nueva Viga, DF
3 20/02/2020 Besugo Veracruz 15.00 19.00 17.00 pescado La Nueva Viga, DF
4 20/02/2020 Cazón con cabeza Veracruz 60.00 65.00 63.00 pescado La Nueva Viga, DF
5 20/02/2020 Huachinango Golfo Tamaulipas 165.00 200.00 190.00 OBS pescado Monterrey, Nuevo León
6 20/02/2020 Pampano Tamaulipas -- -- 195.00 OBS pescado Monterrey, Nuevo León
