I have a data frame with many variables and I want to manage it with a condition to avoid doing that manually.
The data is about farmers' fertilization habits, for each field, the farmer can bring an organic input:
-Date_org, prd_org, QtN_org, mat_epand_org, delai_org
or not with or without a maximum of 4 mineral inputs:
-Date_Nmin1, Prd_Nmin1, QtN_MIN1, Delai_Nmin1, Mode_apport_MIN1, ......... Date_Nmin2 ........ Date_Nmin3 ......... Date_Nmin4 .... Mode_apport_MIN4
I want to fixe a limit to QtN_org, QtN_MIN1, QtN_MIN2, QtN_MIN3, QtN_MIN4 that every time those data are less than 10 for each line in the data frame :
-if QtN_org <10 : i will delete all the data in "Date_org, prd_org, QtN_org, mat_epand_org, delai_org", and i will continu to verify : QtN_MIN1, QtN_MIN2, QtN_MIN3, QtN_MIN4
-if QtN_MIN1 is <10, I am going to delete the data in "Date_Nmin1, Prd_Nmin1, QtN_MIN1, Delai_Nmin1, Mode_apport_MIN1" and shift the following data set by 4 columns so as to have the data present in "Date_Nmin2 at Mode_ Reporting_MIN2" instead of the deleted data, after doing the same thing for the data which will be present in the next 4 columns if "QtN" is <10. -if I delete all the data for the input that I have, I will delete all the line
the idea is to delete all the inputs that have "QtN_org "< 10 and for the mineral ones (QtN_MIN1, QtN_MIN2, QtN_MIN3, QtN_MIN4) every time one of them is < 10 we will replace all its data by the data of the next contribution
This is a small part of the data frame
> dput(df)
structure(list(id = c("1", "2", "3", "4", "5", "6", "7"), region = c("centre",
"centre", "centre", "south", "south", "north", "north"), crop = c("wheat",
"potato", "wheat", "wheat", "corn", "sugar_beet", "sugar_beet"
), date_org = c("12/08/2016", "NA", "24/02/2017", "12/08/2016",
"01/09/2016", "NA", "10/08/2016"), prd_org = c("cattle_manure",
"NA", "sewage_sludge", "manure", "compost", "NA", "compost"),
QtN_org = c("8,4", "NA", "5,2", "46,5", "87,2", "NA", "62,1"
), mat_epand_org = c("spreader", "NA", "burier", "burier",
"spreader", "NA", "hanger ramp"), delai_Org = c(">48h", "NA",
"12-24h", ">48h", "24-48h", "NA", "<4h"), Date_Nmin1 = c("02/03/2017",
"03/05/2017", "22/03/2017", "15/01/2017", "19/04/2017", "14/03/2017",
"NA"), Prd_Nmin1 = c("Ammonitrate", "Ammonitrate", "Ammonitrate",
"Ammonitrate", "Ammonitrate", "nitrogen solution", "NA"),
QtN_MIN1 = c("9,4", "15,6", "4,5", "50,2", "4,8", "54,6",
"NA"), Delai_Nmin1 = c("<4h", "12-24h", "12-24h", "12-24h",
"<4h", "12-24h", "NA"), Mode_apport_MIN1 = c("on the ground",
"on the ground", "on the ground", "on the ground", "on the ground",
"on the ground", "NA"), Date_Nmin2 = c("20/04/2017", "11/05/2017",
"NA", "01/03/2017", "18/06/2017", "NA", "NA"), Prd_Nmin2 = c("nitrogen solution",
"nitrogen solution", "NA", "nitrogen solution", "urea", "NA",
"NA"), QtN_MIN2 = c("71", "6,5", "NA", "5,4", "207", "NA",
"NA"), Delai_Nmin2 = c("12-24h", "12-24h", "NA", "<4h", "immediate",
"NA", "NA"), Mode_apport_MIN2 = c("no landfill", "no landfill",
"NA", "no landfill", "on the ground", "NA", "NA"), Date_Nmin3 = c("03/05/2017",
"01/06/2017", "NA", "NA", "NA", "NA", "NA"), Prd_Nmin3 = c("nitrogen solution",
"Ammonitrate", "NA", "NA", "NA", "NA", "NA"), QtN_MIN3 = c("5.2",
"58,2", "NA", "NA", "NA", "NA", "NA"), Delai_Nmin3 = c("<4h",
"24-48h", "NA", "NA", "NA", "NA", "NA"), Mode_apport_MIN3 = c("no landfill",
"on the ground", "NA", "NA", "NA", "NA", "NA"), Date_Nmin4 = c("16/05/2017",
"NA", "NA", "NA", "NA", "NA", "NA"), Prd_Nmin4 = c("Ammonitrate",
"NA", "NA", "NA", "NA", "NA", "NA"), QtN_MIN4 = c("16,5",
"NA", "NA", "NA", "NA", "NA", "NA"), Delai_Nmin4 = c("24-48h",
"NA", "NA", "NA", "NA", "NA", "NA"), Mode_apport_MIN4 = c("on the ground",
"NA", "NA", "NA", "NA", "NA", "NA")), class = "data.frame", row.names = c(NA,
-7L))
in this small exemple, the result must be like below :
> dput(df2)
structure(list(id = c("1", "2", "4", "5", "6", "7"), region = c("centre",
"centre", "south", "south", "north", "north"), crop = c("wheat",
"potato", "wheat", "corn", "sugar_beet", "sugar_beet"), date_org = c("NA",
"NA", "01/09/2016", "20/03/2017", "NA", "24/02/2017"), prd_org = c("NA",
"NA", "manure", "compost", "NA", "compost"), QtN_org = c("NA",
"NA", "46,5", "87,2", "NA", "62,1"), mat_epand_org = c("NA",
"NA", "burier", "spreader", "NA", "hanger ramp"), delai_Org = c("NA",
"NA", ">48h", "24-48h", "NA", "<4h"), Date_Nmin1 = c("20/04/2017",
"03/05/2017", "15/01/2017", "18/06/2017", "14/03/2017", "NA"),
Prd_Nmin1 = c("nitrogen solution", "Ammonitrate", "Ammonitrate",
"urea", "nitrogen solution", "NA"), QtN_MIN1 = c("71", "15,6",
"50,2", "207", "54,6", "NA"), Delai_Nmin1 = c("12-24h", "12-24h",
"12-24h", "immediate", "12-24h", "NA"), Mode_apport_MIN1 = c("no landfill",
"on the ground", "on the ground", "on the ground", "on the ground",
"NA"), Date_Nmin2 = c("16/05/2017", "01/06/2017", "NA", "NA",
"NA", "NA"), Prd_Nmin2 = c("ammonitrate", "NA", "NA", "NA",
"NA", "NA"), QtN_MIN2 = c("16,5", "58,2", "NA", "NA", "NA",
"NA"), Delai_Nmin2 = c("24-48h", "24-48h", "NA", "NA", "NA",
"NA"), Mode_apport_MIN2 = c("on the ground", "on the ground",
"NA", "NA", "NA", "NA"), Date_Nmin3 = c("NA", "NA", "NA",
"NA", "NA", "NA"), Prd_Nmin3 = c("NA", "NA", "NA", "NA",
"NA", "NA"), QtN_MIN3 = c("NA", "NA", "NA", "NA", "NA", "NA"
), Delai_Nmin3 = c("NA", "NA", "NA", "NA", "NA", "NA"), Mode_apport_MIN3 = c("NA",
"NA", "NA", "NA", "NA", "NA"), Date_Nmin4 = c("NA", "NA",
"NA", "NA", "NA", "NA"), Prd_Nmin4 = c("NA", "NA", "NA",
"NA", "NA", "NA"), QtN_MIN4 = c("NA", "NA", "NA", "NA", "NA",
"NA"), Delai_Nmin4 = c("NA", "NA", "NA", "NA", "NA", "NA"
), Mode_apport_MIN4 = c("NA", "NA", "NA", "NA", "NA", "NA"
)), class = "data.frame", row.names = c(NA, -6L))
if someone can help me for the codes, thanks