简体   繁体   中英

Passing the right arguments in R function

Hi I have a data frame that looks like:

> head(Panel)
   Region Year Population TotalFedTax  BusIncTax IndivIncTax TotalFedSpend
1 Alabama 2000    4452173 18869238000 1541602000 16856323000   37650535757
2 Alabama 2001    4467634 18181072000 1064539000 16779861000   38739545139
3 Alabama 2002    4480089 18650989000 1867639000 16357183000   42052557014
4 Alabama 2003    4503491 17907240000 1472356000 16152532000   45395531847
5 Alabama 2004    4530729 18489339000 1931414000 16205117000   48874307713
6 Alabama 2005    4569805 20398808000 2198098000 17806684000   52057416455

I want to create a function that creates a map of a variable that is created flexibly from the Panel above, in a given year. For example, the map of "TotalFedSpend / Population" in 2000 would be:

temp <- subset(Panel, Year == 2000)
x <- data.frame(temp$Region, temp$TotalFedSpend / temp$Population)
title <- "TotalFedSpend / Population in 2000"
names(x) = c('region','value')
x[,1] = tolower(x[,1])
states <- data.frame(state.center, state.abb)
states_map <- map_data("state")
df = merge(x, states_map, by = "region")
p1 <- ggplot(data = df, aes(x = long, y = lat, group = group))
p1 <- p1 + geom_polygon(aes(fill = cut_number(value, 8)))
p1 <- p1 + geom_path(colour = 'gray', linestyle = 2)
p1 <- p1 + scale_fill_brewer('',palette = 'PuRd')
p1 <- p1 + ggtitle(title)
p1 <- p1 + coord_map()
p1

This code works fine per se, but I am having trouble with creating a function that does this mapping, with flexibly created series. ie I want to create a function that can also map series such as "BusIncTax + IndivIncTax in 2010". I do not know what kind of arguments that I should pass, to specify the variable, and how to fix the 2nd (x <- data.frame...) and 3rd line (title <- ...) inside the function.

Thank you for the help!

temp <- subset(Panel, Year == 2000)
x <- data.frame(temp$Region, temp$TotalFedSpend / temp$Population)


ff <- function(v1, v2, v3, op, data) {
  attach(data)
  tmp = paste(v2,op,v3)
  x = eval(parse(text=tmp))
  return(x)
}

print(ff(Region, TotalFedSpend, Population, "+", temp))
print(ff(Region, TotalFedSpend, Population, "/", temp))
temp <- subset(Panel, Year == 2000)
x <- data.frame(temp$Region, temp$TotalFedSpend / temp$Population)


ff <- function(v1, v2, v3, op, data) {
  attach(data)
  l = paste(v2,op,v3)
  #x = eval(parse(text=l))

  x <- data.frame(v1,eval(parse(text=l)))
  title <- "TotalFedSpend / Population in 2000"
  names(x) = c('region','value')
  x[,1] = tolower(x[,1])
  states <- data.frame(state.center, state.abb)
  states_map <- map_data("state")
  df = merge(x, states_map, by = "region")
  p1 <- ggplot(data = df, aes(x = long, y = lat, group = group))
  p1 <- p1 + geom_path(colour = 'gray', linestyle = 2)
  p1 <- p1 + scale_fill_brewer('',palette = 'PuRd')
  p1 <- p1 + ggtitle(title)
  p1 <- p1 + coord_map()
  p1
  return(p1)
  #return(x)
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM