Are For loops evil in R? -
i've heard you're not meant force procedural programming style onto r. i'm finding pretty hard. i've solved problem loop. wrong? there better, more "r-style" solution?
the problem: have 2 columns: col1 , col2. col1 contains job titles have been entered in free form way. want use col2 collect these job titles categories (so "junior technician", "engineering technician" , "mech. tech." listed "technician".
i've done this:
jobcategories<-list( "junior technician|engineering technician|mech. tech." = "technician", "manager|senior manager|group manager|pain in ****" = "manager", "admin|administrator|group secretary" = "administrator") (currentjob in names(jobcategories)) { df$col2[grep(currentjob,data$col1)] <- jobcategories[[currentjob]] } this produces right results, can't shake feeling (because of procedural experience) i'm not using r properly. r expert put me out of misery?
edit
i asked original data. unfortunately, can't supply it, because it's got confidential info in it. it's 2 columns. first column holds on 400 rows of different job titles (and odd personal name). there 20 different categories these 400 titles can split into. second column starts off na, gets populated after running loop.
for loops not 'evil' in r typically slow compared vector based methods , not best available solution, however easy implement , easy understand , should not under-estimate value of either of these.
in view, therefore, should use for loop if need done , can't see better way and don't need worry speed.
Comments
Post a Comment