I have a dataframe with crime data and associated "prices", organized by country and year (although I don't think this is important here). Here is a subset of my data:
> crime
# A tibble: 8 x 8
iso year theft robbery burglary theft_price robbery_price burglary_price
<chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 ALB 2003 3694 199 874 32.9 115 49.3
2 ALB 2004 3694 199 874 38.2 134 57.3
3 ALB 2005 3694 199 874 42.8 150 64.2
4 ALB 2006 3450 164 779 47.0 165 70.5
5 AUS 2003 722334 14634 586266 408.4 1427 612.4
6 AUS 2004 636717 14634 512551 481.3 1683 721.2
7 AUS 2005 598700 14634 468558 536.7 1877 804.5
8 AUS 2006 594111 14634 433974 564.8 1973 846.5
I want to create new columns that contain the product of each crime type with its price, so theft
x theft_price
= theft_prod
, etc. In my actual dataset I have more crime types so I need something that is scalable to more variables than this subset contains.
I like the syntax of the dplyr
package, so I to use something like this, but I cannot find the solution. I think it is not possible to reference other variables than the ones in vars()
. Correct?
crime %>%
mutate_at(vars(theft, robbery, burglary),
funs(prod = . * ????))
Thanks.