Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 如何获得运行函数的多个结果?_R_Dplyr_Tidyverse_Data Wrangling - Fatal编程技术网

R 如何获得运行函数的多个结果?

R 如何获得运行函数的多个结果?,r,dplyr,tidyverse,data-wrangling,R,Dplyr,Tidyverse,Data Wrangling,我将如何多次运行此功能 我有一个变量叫做percent_people,这是一个变量,如果我们在这个变量的国家有5000000人,还有一个变量叫做city_share,这是一个变量,它关注每个城市的百分比份额,例如伦敦=40%,这个百分比变量有不同的水平,其中有多少人可能失业(即从100%到75%或50%或25%)以及这些不同的百分比如何影响失业率的变化 然而,现在我只能引入一个城市共享和百分之一的人口变量。我如何对其进行编码,以便能够循环使用每个变量的多个输入 现在我有以下几点: library

我将如何多次运行此功能

我有一个变量叫做percent_people,这是一个变量,如果我们在这个变量的国家有5000000人,还有一个变量叫做city_share,这是一个变量,它关注每个城市的百分比份额,例如伦敦=40%,这个百分比变量有不同的水平,其中有多少人可能失业(即从100%到75%或50%或25%)以及这些不同的百分比如何影响失业率的变化

然而,现在我只能引入一个城市共享和百分之一的人口变量。我如何对其进行编码,以便能够循环使用每个变量的多个输入

现在我有以下几点:

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)
库(dplyr)
预测百分比
乘以(人口百分比)

unemp我不确定这是否是您想要的,但是如果您试图让函数同时接受多个
percent\u people
变量,您可以在函数内部循环使用它,以便它可以接受百分比向量:

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)

你是在问你的函数是否同时接受超过百分之一的人的参数?例如,0.5和1?是的,我想这样做,我该怎么做才能让它同时接受超过一个城市的人的参数?嗨,是的,我也想同时接受超过百分之一的人的参数-提醒其他用户如果他们的答案有用的话,我会感谢他们的投票/标记为接受。这不仅是为了我,也是为了每一个以前可能帮助过你的SO同事。请看-
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)