Roci
Roci

Reputation: 13

Novice Trying To Make Sense Of Another Person's R Code

I'm an undergrad trying to essentially recreate someone else's research, and cannot for the life of me right now make sense of this following line of code:

temp_data[, fips := paste0(sprintf("%02d", STATEFP), sprintf("%03d", COUNTYFP))]

temp_data was fread from a csv, and is a "data.table" "data.frame" which I'm reading as either or... The error message that started all of this:

Error in paste0("%02d", STATEFP) : object 'STATEFP' not found

I've looked into both paste0, and sprintf, and am currently thinking that the line of code is trying create STATEFP, and COUNTYFP from temp_data using paste0 after sprintf interprets the fips code however it needs to...

Here is what the temp_data looks like:

screenshot

Any suggestions that can help me to figure out what's going on here would be greatly appreciated. I'm using R 4.0.1 on/with x86_64-apple-darwin17.0 if that helps any.

Upvotes: 0

Views: 62

Answers (1)

Eugene Miller
Eugene Miller

Reputation: 50

Thank you for the screenshot that was very helpful.

sprintf essentially returns a vector containing both text and variable values. It looks like STATEFP and COUNTRYFP must have been defined earlier in the code, most likely vectors. This line of code uses these vectors to filter the data in some way, but I cannot say how without knowing what STATEFP and COUNTRYFP are.

Upvotes: 1

Related Questions