0

I have a data frame with four columns that are each a substring of the first: df$naics_6 has a 6-digit code, df$naics_5 is a substring of the first 5 digits of those 6-digit codes, df$naics_4 and df$naics_3 are similarly configured.

For example,

 naics_6 | naics_5 | naics_4 | naics_3 
---------------------------------------
 325192  | 32519   | 3251    | 325     
 311221  | 31122   | 3112    | 311     
 321113  | 32111   | 3211    | 321     

I'd like to be able to configure my script such that I can input a value that corresponds to the n-digit naics code (e.g. n = 6, n = 5, etc.), pull the matching column, and perform other calculations on it.

The first two lines are where I'm currently at. The third line is where I'm stuck, I'd like to just use str to pull the correct column but it obviously won't work that way.

n = 5
str = paste0('naics_', as.character(n))

new_calc = 5 * df$'str'

Any help would be appreciated!

Kevin M
  • 801
  • 3
  • 9
  • 14

0 Answers0