I know it shows that this question has been already been asked/answered here: (R) [] / subset() returns an empty data frame but it didn't have the solution I was looking for. (My columns do not have padded white space)
So here is my original data
head(d)
County ID event1 event2 row1 row2
Rogers 1 Hearing Application Plea Trial
Rogers 2 Arrest Hearing Application Plea
Rogers 3 Arrest Hearing Plea Disposal
I needed the events and rows columns to all exist in one row.
events <- d %>%
select(County, ID, contains("event"), contains("row")) %>%
gather(m, event, contains("event")) %>%
filter(!is.na(event)) %>%
select(-m)
head(events)
County ID event row1 row2
Rogers 1 Hearing Plea Trial
Rogers 1 Application Plea Trial
Rogers 2 Arrest Application Plea
Rogers 2 Hearing Application Plea
I still needed the row columns as events.
events2 <- events %>%
select(County, ID, event, contains("row")) %>%
gather(m, event, contains("row")) %>%
filter(!is.na(event)) %>%
select(-m)
I hoped it would look like this.
head(events2)
County ID event
Rogers 1 Hearing
Rogers 1 Application
Rogers 1 Plea
Rogers 1 Trial
But instead it returned an empty data frame with 0 observations.
events2
NULL
What am I doing wrong? Thank you!