Skip to main content
Tweeted twitter.com/#!/StackCodeReview/status/559945892526956544
edited title
Link
200_success
  • 144.2k
  • 22
  • 188
  • 473

Returning a column value based on a dataframe argument Lookup GINI by year and country

deleted 24 characters in body; edited title
Source Link
Jamal
  • 34.9k
  • 13
  • 133
  • 237

Is this function grossly inefficient? It takes Returning a while to run.column value based on a dataframe argument

Basically what it does isThis takes a dataframe as an argument, looks at all pairs of two columns in that data frame (country and year) and returns a column value of that pair in another data frame (the gini number).

get_gini <- function(year, country){
    GINI = 0
    SUBSETTED_WITH_COUNTRY_NAME_NOW_WITH_YEAR <- inequality[inequality$Code== country & inequality$Year== year,]
    GINI <- mean(as.numeric(SUBSETTED_WITH_COUNTRY_NAME_NOW_WITH_YEAR$Gini))

    country_and_year<- totes_data[totes_data$totes_data == country & totes_data$year == year,] 
    country_and_year$GiniState1<-GINI
    output<-country_and_year$GiniState1
    output<-output[1]
    if (is.na(output)){
      output=0
    }
    else {
      output = output
    }
    assign("ginilist",output, envir=globalenv())
    rbind(output,ginilist)
}    

Is this function grossly inefficient? It takes a while to run.

Basically what it does is takes a dataframe as an argument, looks all pairs of two columns in that data frame (country and year) and returns a column value of that pair in another data frame (the gini number).

get_gini <- function(year, country){
    GINI = 0
    SUBSETTED_WITH_COUNTRY_NAME_NOW_WITH_YEAR <- inequality[inequality$Code== country & inequality$Year== year,]
    GINI <- mean(as.numeric(SUBSETTED_WITH_COUNTRY_NAME_NOW_WITH_YEAR$Gini))

    country_and_year<- totes_data[totes_data$totes_data == country & totes_data$year == year,] 
    country_and_year$GiniState1<-GINI
    output<-country_and_year$GiniState1
    output<-output[1]
    if (is.na(output)){
      output=0
    }
    else {
      output = output
    }
    assign("ginilist",output, envir=globalenv())
    rbind(output,ginilist)
}    

Returning a column value based on a dataframe argument

This takes a dataframe as an argument, looks at all pairs of two columns in that data frame (country and year) and returns a column value of that pair in another data frame (the gini number).

get_gini <- function(year, country){
    GINI = 0
    SUBSETTED_WITH_COUNTRY_NAME_NOW_WITH_YEAR <- inequality[inequality$Code== country & inequality$Year== year,]
    GINI <- mean(as.numeric(SUBSETTED_WITH_COUNTRY_NAME_NOW_WITH_YEAR$Gini))

    country_and_year<- totes_data[totes_data$totes_data == country & totes_data$year == year,] 
    country_and_year$GiniState1<-GINI
    output<-country_and_year$GiniState1
    output<-output[1]
    if (is.na(output)){
      output=0
    }
    else {
      output = output
    }
    assign("ginilist",output, envir=globalenv())
    rbind(output,ginilist)
}
Source Link
rick
  • 61
  • 1

Is this function grossly inefficient? It takes a while to run.

Basically what it does is takes a dataframe as an argument, looks all pairs of two columns in that data frame (country and year) and returns a column value of that pair in another data frame (the gini number).

get_gini <- function(year, country){
    GINI = 0
    SUBSETTED_WITH_COUNTRY_NAME_NOW_WITH_YEAR <- inequality[inequality$Code== country & inequality$Year== year,]
    GINI <- mean(as.numeric(SUBSETTED_WITH_COUNTRY_NAME_NOW_WITH_YEAR$Gini))

    country_and_year<- totes_data[totes_data$totes_data == country & totes_data$year == year,] 
    country_and_year$GiniState1<-GINI
    output<-country_and_year$GiniState1
    output<-output[1]
    if (is.na(output)){
      output=0
    }
    else {
      output = output
    }
    assign("ginilist",output, envir=globalenv())
    rbind(output,ginilist)
}