0

I am using the tidycensus package to pull out some census variables. I am making a list of desired variables with set variable names (dummy data below). I want to also create a codebook, where, ideally, I'd use the list of variable names to pull the rest of the information from the variable list that you can access with the command load_variable. I'm not sure how to do that join, or pull out that information, just using a character list. Any suggestions?

library("tidycensus")
library("dplyr")
    
decvarlist <- load_variables(2000, "sf1")
desiredvars = c(var1 = "H001001",
                var2 = "H002002",
                var3 = "H002003"
)
     
 #this bit doesnt work, but is sort of how I'm thinking of it  
    codebook <- left_join(desiredvars, decvarlist, by = ())
tchoup
  • 971
  • 4
  • 11

1 Answers1

1

Perhaps we need to filter

library(dplyr)
decvarlist %>%
    filter(name %in% desiredvars) %>%
    mutate(id = names(desiredvars), .before = 1)

-output

# A tibble: 3 × 4
  id    name    label                                concept            
  <chr> <chr>   <chr>                                <chr>              
1 var1  H001001 Total                                HOUSING UNITS [1]  
2 var2  H002002 Total!!Urban                         URBAN AND RURAL [6]
3 var3  H002003 Total!!Urban!!Inside urbanized areas URBAN AND RURAL [6]
akrun
  • 874,273
  • 37
  • 540
  • 662
  • That's great! Can I also do that where there's an additional column for the variable name I gave it. That is to say there's an additional column on the left that is var1, var2, var3 or whatever I named it? – tchoup Oct 25 '22 at 15:17
  • @akrun please explain the need for `.before =1` – IRTFM Oct 25 '22 at 15:26