I extracted a table from a PDF file with extract_tables(), but the text has been spread out across multiple rows. The number of rows varies per record. I would like to combine the text into a single value.
What I would like to do is similar to this post. The difference is that I have text in multiple columns. The number of records that each entry uses is variable, depending on a different column each time.
Example: One entry may take up four rows because the "Name & location" column is spread out across four rows, (while the other columns only take up two rows for that entry; the rest is filled with NA). For another entry, the text may be spread out across 6 rows, due to the length of the text in the "Expertise" column.
A new record starts every time when the "Level" column contains a value, rather than NA. Edit: the "Level" values are non-unique
My data looks like this:
Name & location Expertise Type Sector Payment Level
1: Ms. Jane Student Higher Government and payment 1
2: Doe, <NA> Education education has been <NA>
3: NUS <NA> institute <NA> received <NA>
4: Andrew Saunders Phd., Chief Municipal Government and payment 5
5: Municipality of Education government education has not <NA>
6: Amsterdam Officer <NA> <NA> been <NA>
7: <NA> <NA> <NA> <NA> received <NA>
8: Mr. Stephen Spokesperson for Municipal Government and payment 3
9: Johnson, Sustainability, government education has not <NA>
10: Orange County Health & <NA> <NA> been <NA>
11: <NA> Wellbeing and <NA> <NA> received <NA>
12: <NA> Wellfare <NA> <NA> <NA> <NA>
13: Mrs. Susan Junior national Government and payment 4
14: Andrews, Research government education has not <NA>
15: Police Manager <NA> <NA> been <NA>
16: <NA> Money <NA> <NA> received <NA>
17: <NA> Laundering <NA> <NA> <NA> <NA>
Reproducible Example:
structure(list(`Name & location` = c("1: Ms. Jane", "2: Doe,",
"3: NUS", "4: Andrew Saunders Phd.,", "5: Municipality of",
"6: Amsterdam", "7: <NA>", "8: Mr. Stephen", "9: Johnson,",
"10: Orange County", "11: <NA>", "12: <NA>", "13: Mrs. Susan",
"14: Andrews,", "15: Police", "16: <NA>", "17: <NA>"),
Expertise = c("Student", NA, NA, "Chief", "Education", "Officer",
NA, "Spokesperson for", "Sustainability,", "Health &", "Wellbeing and",
"Wellfare", "Junior", "Research", "Manager", "Money", "Laundering"
), Type = c("Higher", "Education", "Insititute", "Municipal",
"Government", NA, NA, "Municipal", "Government", NA, NA,
NA, "National", "Government", NA, NA, NA), Sector = c("Government and",
"education", NA, "Government and", "education", NA, NA, "Government and",
"education", NA, NA, NA, "Government and", "education", NA,
NA, NA), Payment = c("payment", "has been", "received", "Payment",
"has not", "been", "received", "Payment", "has not", "been",
"received", NA, "Payment", "has not", "been", "received",
NA), Level = c(1, NA, NA, 5, NA, NA, NA, 3, NA, NA, NA, NA,
4, NA, NA, NA, NA)), row.names = c(NA, -17L), class = c("tbl_df",
"tbl", "data.frame"))
What I tried so far is different versions of the code below
DF_clean <- DF %>% mutate(Level = ifelse(grepl(NA, Level))) %>%
group_by(id = cumsum(!is.na(Level))) %>%
mutate(Level = first(Level)) %>%
group_by(Level) %>%
summarise(Name = paste(Name, collapse = " "),
Expertise = paste(Expertise, collapse = " "),
Type = paste(Type, collapse = " "),
Sector = paste(Sector, collapse = " "),
Level = paste(Level, collapse = " "))
But this seems to collapse all text into a single record.
Any ideas on how to solve this?