简体   繁体   English

如何在观察上运行 function 获得多个结果?

[英]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?我有一个名为 percent_people 的变量,它查看变量国家/地区是否有 5000000 人,并且有一个名为 city_share 的变量查看每个城市的百分比份额,例如 London = 40%,百分比变量具有不同级别的多少他们可能会失业(即从 100% 到 75% 或 50% 或 25%),这些不同的百分比会如何影响失业率的变化?

However, right now I can only introduce one city_share and one percent_people variables.但是,现在我只能引入一个 city_share 和一个 percent_people 变量。 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:我不确定这是否是你想要的,但如果你试图让 function 一次接受超过一个percent_people变量,你可以在 function 内部循环它,以便它可以接受百分比向量:

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.如果您希望它还返回几个city_share输入的结果,我认为您可能需要切换到列表。 The code below may not be perfect, but it does the job of returning one list of values per city_share introduced.下面的代码可能并不完美,但它确实可以为每个引入的 city_share 返回一个值列表。

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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