在 R 函数中传递正确的参数
Passing the right arguments in R function
您好,我有一个数据框,如下所示:
> 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
我想创建一个函数来创建一个变量的映射,该变量是在给定年份从上面的面板灵活创建的。例如,2000 年 "TotalFedSpend / Population" 的地图将是:
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
此代码本身工作正常,但我在创建一个执行此映射的函数时遇到了问题,该函数具有灵活创建的系列。即我想创建一个函数,它也可以映射 "BusIncTax + IndivIncTax in 2010" 等系列。我不知道我应该传递什么样的参数来指定变量,以及如何修复第二行 (x <- data.frame...) 和第三行 (title <- ...)功能。
感谢您的帮助!
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)
}
您好,我有一个数据框,如下所示:
> 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
我想创建一个函数来创建一个变量的映射,该变量是在给定年份从上面的面板灵活创建的。例如,2000 年 "TotalFedSpend / Population" 的地图将是:
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
此代码本身工作正常,但我在创建一个执行此映射的函数时遇到了问题,该函数具有灵活创建的系列。即我想创建一个函数,它也可以映射 "BusIncTax + IndivIncTax in 2010" 等系列。我不知道我应该传递什么样的参数来指定变量,以及如何修复第二行 (x <- data.frame...) 和第三行 (title <- ...)功能。
感谢您的帮助!
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)
}