简体   繁体   中英

how to get multiple outcomes for running a function on observations?

how would i run this multiple times?

I have a variable called percent_people, which looks if we have 5000000 people in the variable country, and have a variable called city_share which looks at the percentage share per city, eg London = 40%, the percent variable has different levels of how many of them could potentially become unemployed (ie ranging from 100% to 75% or 50% or 25%), and how these different percentages could affect the how the unemployment rate will change?

However, right now I can only introduce one city_share and one percent_people variables. How could I code it so that I can loop through more than one input of each of the variables?

Right now I have the following:

library(dplyr)

Prediction <- function(city_share,
                       percent_people) {
      unemployed_lon <-5000000 %>% 
        multiply_by(city_share) %>%
        multiply_by(percent_people)

      unemp <- 100000 +unemployed_lon

      unemprate <- unemp %>% divide_by(5000000)

      return(unemprate)
    }

# Check -0.4 share + 100% percent_people

Prediction(0.4,1)

I am not sure if this is what you want, but if you are trying to make the function accept more than one percent_people variable at once, you can loop through it inside of the function so that it can accept a vector of percentages:

library(dplyr)
library(magrittr)

Prediction <- function(city_share,
                       percent_people) {
  unemprates <- c()
  for (i in percent_people){
    unemployed_lon <-5000000 %>% 
      multiply_by(city_share) %>%
      multiply_by(percent_people)
    unemp <- 100000 +unemployed_lon
    unemprate <- unemp %>% divide_by(5000000)
  }
  return(unemprate)
}

# Check -0.4 share + 100% percent_people

Prediction(0.4,c(1,0.5,0.25))

Prediction(0.4,1)

If you want it to also return the result of several city_share inputs, I think what you probably need is to switch to lists. The code below may not be perfect, but it does the job of returning one list of values per city_share introduced.

library(dplyr)
library(magrittr)

Prediction <- function(city_share,
                       percent_people) {

  unemprates_all<-list()

  for (i in city_share){
    unemp_share <- c()
        for (j in percent_people){

          unemployed_lon <-5000000 %>% 
          multiply_by(i) %>%
          multiply_by(j)
          unemp <- 100000 + unemployed_lon
          unemp <- unemp %>% divide_by(5000000)
          unemp_share <- append(unemp_share,unemp)

        }
    unemprate <- list(unemp_share)
    unemprates_all[[length(unemprates_all)+1]] <- unemprate
  } 
 return(unemprates_all)
}

# Check -0.4 share + 100% percent_people

Prediction(c(0.4,0.2),c(1,0.5))

Prediction(0.4,1)

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