-1

I'm trying to create a function in R that takes two inputs, the dataset name and a column from the dataset. I want to display all the continents and the total population of the continents when the dataset name and the column is inserted as parameters. I'm currently using the code below but it displays all the continents with 0 population. How can I display the total population next to each continent instead of 0. Any help will be greatly appreciated, thanks.

continentLifeExp <- function(data, column){
    continents <- group_by(data, Continent)
    summarise(continents, population = sum(data$`column`))
}
GleneaMan
  • 167
  • 1
  • 2
  • 13
  • Please make your code reproducible by providing (minimal!) data. Use `dput(yourDataframe)` and put the result in your question, i.e. edit your question: http://stackoverflow.com/posts/43055763/edit – jogo Mar 27 '17 at 20:25

2 Answers2

2

Try this data[[column]]

As long as column is a string, this should work fine.

 sum(data[[column]])
msubbaiah
  • 350
  • 2
  • 14
2

Assuming you want the population per contintent you should be able to just use

summarise_(continents, population = paste0("sum(",column, ")"))

summarize_ evaluates expressions in strings

Robin Gertenbach
  • 10,316
  • 3
  • 25
  • 37