Hello, I am trying to copy data from one row to another (or several) to replace NA's based on matching ID's. I have part of the dataset presented below.
data.frame(
stringsAsFactors = FALSE,
check.names = FALSE,
`Date of survey` = c("20/10/21",
"20/10/21","19-10-2021","19-10-2021","19-10-2021",
"19-10-2021","19-10-2021","19-10-2021",
"19-10-2021","19-10-2021","19-10-2021","19-10-2021",
"19-10-2021","19-10-2021"),
`Catchment area` = c("Mndoliro",
"Mndoliro","Mndoliro","Mndoliro","Mndoliro",
"Mndoliro","Mndoliro","Mndoliro","Mndoliro",
"Mndoliro","Mndoliro","Mndoliro","Mndoliro",
"Mndoliro"),
`Village name` = c("Chezi 1",
"Chezi 1","Chezi 1","Chezi 1","Chezi 1","Chezi 1",
"Chezi 1","Chezi 1","Chezi 1","Chezi 1",
"Chezi 1","Chezi 1","Chezi 1","Chezi 1"),
Chief = c("Chezi 1",
"Chezi 1","Chezi1","chezi1",NA,"Chezi1",NA,NA,
"Chezi1","Chezi1","Chezi1","Chezi1",NA,NA),
`# of houses in village` = c(187,NA,187,
187,NA,187,NA,NA,187,187,187,187,NA,NA),
`CDS code` = c("CDS/A/XXX",
"CDS/A/XXX","CDS/A/YYY","CDS/A/001","CDS/A/001",
"CDS/A/002","CDS/A/002","CDS/A/002",
"CDS/A/003","CDS/A/004","CDS/A/005","CDS/A/006",
"CDS/A/006","CDS/A/006"),
`Name of participant` = c("James dandy",
NA,"Cathy leggo","Kamy limomo",NA,
"Anny Chikupepe",NA,NA,"F Chauwadidi","G Chauwadidi",
"Lovely acar","Jose Chipper",NA,NA),
`Head of house` = c("Yes",NA,
"Yes","No",NA,"No",NA,NA,"Yes","No","No",
"No",NA,NA),
`# of people in HH` = c(4, NA, 3, 6, NA, 6, NA, NA, 2, 2, 5, 6, NA, NA)
)
#> Date of survey Catchment area Village name Chief # of houses in village
#> 1 20/10/21 Mndoliro Chezi 1 Chezi 1 187
#> 2 20/10/21 Mndoliro Chezi 1 Chezi 1 NA
#> 3 19-10-2021 Mndoliro Chezi 1 Chezi1 187
#> 4 19-10-2021 Mndoliro Chezi 1 chezi1 187
#> 5 19-10-2021 Mndoliro Chezi 1 <NA> NA
#> 6 19-10-2021 Mndoliro Chezi 1 Chezi1 187
#> 7 19-10-2021 Mndoliro Chezi 1 <NA> NA
#> 8 19-10-2021 Mndoliro Chezi 1 <NA> NA
#> 9 19-10-2021 Mndoliro Chezi 1 Chezi1 187
#> 10 19-10-2021 Mndoliro Chezi 1 Chezi1 187
#> 11 19-10-2021 Mndoliro Chezi 1 Chezi1 187
#> 12 19-10-2021 Mndoliro Chezi 1 Chezi1 187
#> 13 19-10-2021 Mndoliro Chezi 1 <NA> NA
#> 14 19-10-2021 Mndoliro Chezi 1 <NA> NA
#> CDS code Name of participant Head of house # of people in HH
#> 1 CDS/A/XXX James dandy Yes 4
#> 2 CDS/A/XXX <NA> <NA> NA
#> 3 CDS/A/YYY Cathy leggo Yes 3
#> 4 CDS/A/001 Kamy limomo No 6
#> 5 CDS/A/001 <NA> <NA> NA
#> 6 CDS/A/002 Anny Chikupepe No 6
#> 7 CDS/A/002 <NA> <NA> NA
#> 8 CDS/A/002 <NA> <NA> NA
#> 9 CDS/A/003 F Chauwadidi Yes 2
#> 10 CDS/A/004 G Chauwadidi No 2
#> 11 CDS/A/005 Lovely acar No 5
#> 12 CDS/A/006 Jose Chipper No 6
#> 13 CDS/A/006 <NA> <NA> NA
#> 14 CDS/A/006 <NA> <NA> NA
To put it in context, in every household "CDS code" , multiple entries were made to represent each child, but for the second and/or third entry, only the ID was entered and a few other variables. I want to copy the other information from the first entry to the others so that it appears as below. Please note that in the real data the data ID's are mixed up so the "copy from above row" option may not work. I haven't been able to find a better solution
data.frame(
stringsAsFactors = FALSE,
check.names = FALSE,
`Date of survey` = c("20/10/21",
"20/10/21","19-10-2021","19-10-2021","19-10-2021",
"19-10-2021","19-10-2021","19-10-2021",
"19-10-2021","19-10-2021","19-10-2021","19-10-2021",
"19-10-2021","19-10-2021"),
`Catchment area` = c("Mndoliro",
"Mndoliro","Mndoliro","Mndoliro","Mndoliro",
"Mndoliro","Mndoliro","Mndoliro","Mndoliro",
"Mndoliro","Mndoliro","Mndoliro","Mndoliro",
"Mndoliro"),
`Village name` = c("Chezi 1",
"Chezi 1","Chezi 1","Chezi 1","Chezi 1","Chezi 1",
"Chezi 1","Chezi 1","Chezi 1","Chezi 1",
"Chezi 1","Chezi 1","Chezi 1","Chezi 1"),
Chief = c("Chezi 1",
"Chezi 1","Chezi1","Chezi2","Chezi3","Chezi4",
"Chezi5","Chezi6","Chezi7","Chezi8","Chezi9",
"Chezi10","Chezi11","Chezi12"),
`# of houses in village` = c(187,187,187,
187,187,187,187,187,187,187,187,187,187,
187),
`CDS code` = c("CDS/A/XXX",
"CDS/A/XXX","CDS/A/YYY","CDS/A/001","CDS/A/001",
"CDS/A/002","CDS/A/002","CDS/A/002",
"CDS/A/003","CDS/A/004","CDS/A/005","CDS/A/006",
"CDS/A/006","CDS/A/006"),
`Name of participant` = c("James dandy",
"James dandy","Cathy leggo","Kamy limomo",
"Kamy limomo","Anny Chikupepe","Anny Chikupepe",
"Anny Chikupepe","F Chauwadidi","G Chauwadidi",
"Lovely acar","Jose Chipper","Jose Chipper",
"Jose Chipper"),
`Head of house` = c("Yes","Yes",
"Yes","No","No","No","No","No","Yes","No",
"No","No","No","No"),
`# of people in HH` = c(4, 4, 3, 6, 6, 6, 6, 6, 2, 2, 5, 6, 6, 6)
)
#> Date of survey Catchment area Village name Chief # of houses in village
#> 1 20/10/21 Mndoliro Chezi 1 Chezi 1 187
#> 2 20/10/21 Mndoliro Chezi 1 Chezi 1 187
#> 3 19-10-2021 Mndoliro Chezi 1 Chezi1 187
#> 4 19-10-2021 Mndoliro Chezi 1 Chezi2 187
#> 5 19-10-2021 Mndoliro Chezi 1 Chezi3 187
#> 6 19-10-2021 Mndoliro Chezi 1 Chezi4 187
#> 7 19-10-2021 Mndoliro Chezi 1 Chezi5 187
#> 8 19-10-2021 Mndoliro Chezi 1 Chezi6 187
#> 9 19-10-2021 Mndoliro Chezi 1 Chezi7 187
#> 10 19-10-2021 Mndoliro Chezi 1 Chezi8 187
#> 11 19-10-2021 Mndoliro Chezi 1 Chezi9 187
#> 12 19-10-2021 Mndoliro Chezi 1 Chezi10 187
#> 13 19-10-2021 Mndoliro Chezi 1 Chezi11 187
#> 14 19-10-2021 Mndoliro Chezi 1 Chezi12 187
#> CDS code Name of participant Head of house # of people in HH
#> 1 CDS/A/XXX James dandy Yes 4
#> 2 CDS/A/XXX James dandy Yes 4
#> 3 CDS/A/YYY Cathy leggo Yes 3
#> 4 CDS/A/001 Kamy limomo No 6
#> 5 CDS/A/001 Kamy limomo No 6
#> 6 CDS/A/002 Anny Chikupepe No 6
#> 7 CDS/A/002 Anny Chikupepe No 6
#> 8 CDS/A/002 Anny Chikupepe No 6
#> 9 CDS/A/003 F Chauwadidi Yes 2
#> 10 CDS/A/004 G Chauwadidi No 2
#> 11 CDS/A/005 Lovely acar No 5
#> 12 CDS/A/006 Jose Chipper No 6
#> 13 CDS/A/006 Jose Chipper No 6
#> 14 CDS/A/006 Jose Chipper No 6
Apologies for the long post