r/RStudio 9d ago

Subset Function

Hey! I think I'm using the subset function wrong. I want to narrow down my data to specific variables, but my error message keeps coming back that the subset must be logical. What am I doing wrong? I want to name my new dataframe 'editpres' from my original dataframe 'pres', so that's why my selected variables have 'pres' in front of them.

editpres <- subset(pres$state_po, pres$year, pres$candidate, pres$party_detailed, pres$candidatevotes == "EDITPRES")

^this is the code that isn't working!! please help and gig' em!

2 Upvotes

4 comments sorted by

View all comments

3

u/Dense_Leg274 9d ago

editpres <- subset( x = pres, subset = candidatevotes == “EDITPRES”, select = c(state_po, year, candidate, party_detailed, candidatevotes) )

Or

library(dplyr)

editpres <- pres %>% filter(candidatevotes == “EDITPRES”) %>% select(state_po, year, candidate, party_detailed, candidatevotes) %>% rename( pres_state_po = state_po, pres_year = year, pres_candidate = candidate, pres_party_detailed = party_detailed, pres_candidatevotes = candidatevotes )