Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/70.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_Function_Functional Programming - Fatal编程技术网

有没有办法让R代码更有效?

有没有办法让R代码更有效?,r,function,functional-programming,R,Function,Functional Programming,我正在应用一个生育率模型,为了执行该模型,我需要为每个生育强度保存矩阵,我称之为mati,根据其顺序。在这种情况下,i=(1,2,3,…n) 下面的数据框是如何显示我的数据的示例。我的真实数据帧有525行和10列(“年龄”“年份”“mat1”“mat2”“mat3”“mat4”“mat5”“mat6”“mat7”“mat8”) 我也开始写循环,但我已经卡在第一行了 mat_list <- list() for(i in names(mat[,3:7])) { mat_list[[i]]

我正在应用一个生育率模型,为了执行该模型,我需要为每个生育强度保存矩阵,我称之为
mati
,根据其顺序。在这种情况下,i=(1,2,3,…n) 下面的数据框是如何显示我的数据的示例。我的真实数据帧有525行和10列(
“年龄”“年份”“mat1”“mat2”“mat3”“mat4”“mat5”“mat6”“mat7”“mat8”

我也开始写循环,但我已经卡在第一行了

mat_list <- list()
for(i in names(mat[,3:7])) {
  mat_list[[i]] <- data.frame(
                      spread(
                        select(mat, AGE, year, mat[[paste0("mat",i)]]), year, mat[[paste0("mat", i)]])) 

我相信您希望
收集
,然后
传播
数据。这允许您通过两个步骤完成所有操作

library(dplyr)
library(tidyr)

mat %>%
  gather(key, value, -year, -Age)%>%
  spread(year, value)%>%
  group_split(key)

[[1]]
# A tibble: 4 x 6
    Age key   `1998` `1999` `2000` `2001`
  <int> <chr>        <dbl>        <dbl>        <dbl>        <dbl>
1    15 mat1          0.01         0.01         0.01         0.01
2    16 mat1          0.01         0.01         0.01         0.01
3    17 mat1          0.01         0.01         0.01         0.01
4    18 mat1          0.01         0.01         0.01         0.01

[[2]]
# A tibble: 4 x 6
    Age key   `1998` `1999` `2000` `2001`
  <int> <chr>        <dbl>        <dbl>        <dbl>        <dbl>
1    15 mat2         0.012        0.012        0.012        0.012
2    16 mat2         0.012        0.012        0.012        0.012
3    17 mat2         0.012        0.012        0.012        0.012
4    18 mat2         0.012        0.012        0.012        0.012

[[3]]
# A tibble: 4 x 6
    Age key   `1998` `1999` `2000` `2001`
  <int> <chr>        <dbl>        <dbl>        <dbl>        <dbl>
1    15 mat3         0.015        0.015        0.015        0.015
2    16 mat3         0.015        0.015        0.015        0.015
3    17 mat3         0.015        0.015        0.015        0.015
4    18 mat3         0.015        0.015        0.015        0.015
库(dplyr)
图书馆(tidyr)
材料%>%
聚集(键、值、-年、-年龄)%>%
价差(年份、价值)%>%
组分割(键)
[[1]]
#一个tibble:4x6
年龄键'1998``1999``2000``2001`
1 15 mat1 0.01 0.01 0.01 0.01
2 16 mat1 0.01 0.01 0.01 0.01
3 17 mat1 0.01 0.01 0.01 0.01
4 18 mat1 0.01 0.01 0.01 0.01
[[2]]
#一个tibble:4x6
年龄键'1998``1999``2000``2001`
1 15 mat2 0.012 0.012 0.012 0.012
2 16 mat2 0.012 0.012 0.012 0.012
3 17 mat2 0.012 0.012 0.012 0.012
4 18 mat2 0.012 0.012 0.012 0.012
[[3]]
#一个tibble:4x6
年龄键'1998``1999``2000``2001`
1 15 mat3 0.015 0.015 0.015 0.015 0.015
2 16 mat3 0.015 0.015 0.015 0.015 0.015
3 17 mat3 0.015 0.015 0.015 0.015 0.015
4 18 mat3 0.015 0.015 0.015 0.015 0.015
或者您可以在base中执行此操作:

mats <- reshape(data = data.frame(year = mat$year,Age = mat$Age,  stack(mat, select = c('mat1', 'mat2', 'mat3')))
        , idvar = c('Age', 'ind'), timevar = c('year'), direction = 'wide')

mat_list <- split(mats, mats$ind)

mat_list

$mat1
  Age  ind values.1998 values.1999 values.2000 values.2001
1  15 mat1        0.01        0.01        0.01        0.01
2  16 mat1        0.01        0.01        0.01        0.01
3  17 mat1        0.01        0.01        0.01        0.01
4  18 mat1        0.01        0.01        0.01        0.01

$mat2
   Age  ind values.1998 values.1999 values.2000 values.2001
17  15 mat2       0.012       0.012       0.012       0.012
18  16 mat2       0.012       0.012       0.012       0.012
19  17 mat2       0.012       0.012       0.012       0.012
20  18 mat2       0.012       0.012       0.012       0.012

$mat3
   Age  ind values.1998 values.1999 values.2000 values.2001
33  15 mat3       0.015       0.015       0.015       0.015
34  16 mat3       0.015       0.015       0.015       0.015
35  17 mat3       0.015       0.015       0.015       0.015
36  18 mat3       0.015       0.015       0.015       0.015

mats我相信你想
收集
然后
传播
数据。这允许您通过两个步骤完成所有操作

library(dplyr)
library(tidyr)

mat %>%
  gather(key, value, -year, -Age)%>%
  spread(year, value)%>%
  group_split(key)

[[1]]
# A tibble: 4 x 6
    Age key   `1998` `1999` `2000` `2001`
  <int> <chr>        <dbl>        <dbl>        <dbl>        <dbl>
1    15 mat1          0.01         0.01         0.01         0.01
2    16 mat1          0.01         0.01         0.01         0.01
3    17 mat1          0.01         0.01         0.01         0.01
4    18 mat1          0.01         0.01         0.01         0.01

[[2]]
# A tibble: 4 x 6
    Age key   `1998` `1999` `2000` `2001`
  <int> <chr>        <dbl>        <dbl>        <dbl>        <dbl>
1    15 mat2         0.012        0.012        0.012        0.012
2    16 mat2         0.012        0.012        0.012        0.012
3    17 mat2         0.012        0.012        0.012        0.012
4    18 mat2         0.012        0.012        0.012        0.012

[[3]]
# A tibble: 4 x 6
    Age key   `1998` `1999` `2000` `2001`
  <int> <chr>        <dbl>        <dbl>        <dbl>        <dbl>
1    15 mat3         0.015        0.015        0.015        0.015
2    16 mat3         0.015        0.015        0.015        0.015
3    17 mat3         0.015        0.015        0.015        0.015
4    18 mat3         0.015        0.015        0.015        0.015
库(dplyr)
图书馆(tidyr)
材料%>%
聚集(键、值、-年、-年龄)%>%
价差(年份、价值)%>%
组分割(键)
[[1]]
#一个tibble:4x6
年龄键'1998``1999``2000``2001`
1 15 mat1 0.01 0.01 0.01 0.01
2 16 mat1 0.01 0.01 0.01 0.01
3 17 mat1 0.01 0.01 0.01 0.01
4 18 mat1 0.01 0.01 0.01 0.01
[[2]]
#一个tibble:4x6
年龄键'1998``1999``2000``2001`
1 15 mat2 0.012 0.012 0.012 0.012
2 16 mat2 0.012 0.012 0.012 0.012
3 17 mat2 0.012 0.012 0.012 0.012
4 18 mat2 0.012 0.012 0.012 0.012
[[3]]
#一个tibble:4x6
年龄键'1998``1999``2000``2001`
1 15 mat3 0.015 0.015 0.015 0.015 0.015
2 16 mat3 0.015 0.015 0.015 0.015 0.015
3 17 mat3 0.015 0.015 0.015 0.015 0.015
4 18 mat3 0.015 0.015 0.015 0.015 0.015
或者您可以在base中执行此操作:

mats <- reshape(data = data.frame(year = mat$year,Age = mat$Age,  stack(mat, select = c('mat1', 'mat2', 'mat3')))
        , idvar = c('Age', 'ind'), timevar = c('year'), direction = 'wide')

mat_list <- split(mats, mats$ind)

mat_list

$mat1
  Age  ind values.1998 values.1999 values.2000 values.2001
1  15 mat1        0.01        0.01        0.01        0.01
2  16 mat1        0.01        0.01        0.01        0.01
3  17 mat1        0.01        0.01        0.01        0.01
4  18 mat1        0.01        0.01        0.01        0.01

$mat2
   Age  ind values.1998 values.1999 values.2000 values.2001
17  15 mat2       0.012       0.012       0.012       0.012
18  16 mat2       0.012       0.012       0.012       0.012
19  17 mat2       0.012       0.012       0.012       0.012
20  18 mat2       0.012       0.012       0.012       0.012

$mat3
   Age  ind values.1998 values.1999 values.2000 values.2001
33  15 mat3       0.015       0.015       0.015       0.015
34  16 mat3       0.015       0.015       0.015       0.015
35  17 mat3       0.015       0.015       0.015       0.015
36  18 mat3       0.015       0.015       0.015       0.015

mats扩展科尔的答案

mat %>%
    gather("mat", "val", -year, -Age) %>%
    mutate(Age=paste("age",Age), year=paste("year",year)) %>%
    group_by(mat) %>%
    group_map(~spread(., year, val))
purrr::group_映射将函数应用于每个组,并返回一个列表,其中每个列表元素都是应用于每个组的函数的结果

# A tibble: 4 x 5
  Age    `year 1998` `year 1999` `year 2000` `year 2001`
  <chr>        <dbl>       <dbl>       <dbl>       <dbl>
1 age 15        0.01        0.01        0.01        0.01
2 age 16        0.01        0.01        0.01        0.01
3 age 17        0.01        0.01        0.01        0.01
4 age 18        0.01        0.01        0.01        0.01

[[2]]
# A tibble: 4 x 5
  Age    `year 1998` `year 1999` `year 2000` `year 2001`
  <chr>        <dbl>       <dbl>       <dbl>       <dbl>
1 age 15       0.012       0.012       0.012       0.012
2 age 16       0.012       0.012       0.012       0.012
3 age 17       0.012       0.012       0.012       0.012
4 age 18       0.012       0.012       0.012       0.012

[[3]]
# A tibble: 4 x 5
  Age    `year 1998` `year 1999` `year 2000` `year 2001`
  <chr>        <dbl>       <dbl>       <dbl>       <dbl>
1 age 15       0.015       0.015       0.015       0.015
2 age 16       0.015       0.015       0.015       0.015
3 age 17       0.015       0.015       0.015       0.015
4 age 18       0.015       0.015       0.015       0.015
#一个tible:4 x 5
年龄` 1998年` 1999年` 2000年` 2001年`
1岁15 0.01 0.01 0.01 0.01 0.01
2岁16 0.01 0.01 0.01 0.01 0.01
3岁17 0.01 0.01 0.01 0.01 0.01
4岁18 0.01 0.01 0.01 0.01 0.01
[[2]]
#一个tibble:4x5
年龄` 1998年` 1999年` 2000年` 2001年`
1年龄15 0.012 0.012 0.012 0.012 0.012
2岁16 0.012 0.012 0.012 0.012 0.012
3岁17 0.012 0.012 0.012 0.012 0.012
4岁18 0.012 0.012 0.012 0.012 0.012
[[3]]
#一个tibble:4x5
年龄` 1998年` 1999年` 2000年` 2001年`
1岁15 0.015 0.015 0.015 0.015 0.015
2岁16 0.015 0.015 0.015 0.015 0.015
3岁17 0.015 0.015 0.015 0.015 0.015
4岁18 0.015 0.015 0.015 0.015 0.015
这是使用科尔稍加修改的数据

year <- rep(1998:2001, each = 4) #each was the change.
Age <- rep(15:18, 4)
mat1 <- rep(0.01, 16)
mat2 <- rep(0.012, 16)
mat3 <- rep(0.015, 16)
mat <- data.frame(year, Age, mat1, mat2, mat3)

year来扩展科尔的答案

mat %>%
    gather("mat", "val", -year, -Age) %>%
    mutate(Age=paste("age",Age), year=paste("year",year)) %>%
    group_by(mat) %>%
    group_map(~spread(., year, val))
purrr::group_映射将函数应用于每个组,并返回一个列表,其中每个列表元素都是应用于每个组的函数的结果

# A tibble: 4 x 5
  Age    `year 1998` `year 1999` `year 2000` `year 2001`
  <chr>        <dbl>       <dbl>       <dbl>       <dbl>
1 age 15        0.01        0.01        0.01        0.01
2 age 16        0.01        0.01        0.01        0.01
3 age 17        0.01        0.01        0.01        0.01
4 age 18        0.01        0.01        0.01        0.01

[[2]]
# A tibble: 4 x 5
  Age    `year 1998` `year 1999` `year 2000` `year 2001`
  <chr>        <dbl>       <dbl>       <dbl>       <dbl>
1 age 15       0.012       0.012       0.012       0.012
2 age 16       0.012       0.012       0.012       0.012
3 age 17       0.012       0.012       0.012       0.012
4 age 18       0.012       0.012       0.012       0.012

[[3]]
# A tibble: 4 x 5
  Age    `year 1998` `year 1999` `year 2000` `year 2001`
  <chr>        <dbl>       <dbl>       <dbl>       <dbl>
1 age 15       0.015       0.015       0.015       0.015
2 age 16       0.015       0.015       0.015       0.015
3 age 17       0.015       0.015       0.015       0.015
4 age 18       0.015       0.015       0.015       0.015
#一个tible:4 x 5
年龄` 1998年` 1999年` 2000年` 2001年`
1岁15 0.01 0.01 0.01 0.01 0.01
2岁16 0.01 0.01 0.01 0.01 0.01
3岁17 0.01 0.01 0.01 0.01 0.01
4岁18 0.01 0.01 0.01 0.01 0.01
[[2]]
#一个tibble:4x5
年龄` 1998年` 1999年` 2000年` 2001年`
1年龄15 0.012 0.012 0.012 0.012 0.012
2岁16 0.012 0.012 0.012 0.012 0.012
3岁17 0.012 0.012 0.012 0.012 0.012
4岁18 0.012 0.012 0.012 0.012 0.012
[[3]]
#一个tibble:4x5
年龄` 1998年` 1999年` 2000年` 2001年`
1岁15 0.015 0.015 0.015 0.015 0.015
2岁16 0.015 0.015 0.015 0.015 0.015
3岁17 0.015 0.015 0.015 0.015 0.015
4岁18 0.015 0.015 0.015 0.015 0.015
这是使用科尔稍加修改的数据

year <- rep(1998:2001, each = 4) #each was the change.
Age <- rep(15:18, 4)
mat1 <- rep(0.01, 16)
mat2 <- rep(0.012, 16)
mat3 <- rep(0.015, 16)
mat <- data.frame(year, Age, mat1, mat2, mat3)

year首次重塑为long

#add unique id to your data
mat$id=1:nrow(mat)
#reshape to long by mat
long1 = reshape_toLong(data = mat,id = "id",j = "all123",value.var.prefix = "mat")
#delet id column
long2=long1[,-1]
第二次resha
#reshape wide by year
wide=reshape_toWide(data = long2,id = "all123",j = "year",value.var.prefix = "mat")
wide[wide$all123==1,]
   Age all123 mat1998 mat1999 mat2000 mat2001
1   15      1    0.01    0.01    0.01    0.01
4   16      1    0.01    0.01    0.01    0.01
8   17      1    0.01    0.01    0.01    0.01
12  18      1    0.01    0.01    0.01    0.01
wide[wide$all123==2,]
   Age all123 mat1998 mat1999 mat2000 mat2001
3   15      2   0.012   0.012   0.012   0.012
5   16      2   0.012   0.012   0.012   0.012
7   17      2   0.012   0.012   0.012   0.012
11  18      2   0.012   0.012   0.012   0.012
wide[wide$all123==3,]
   Age all123 mat1998 mat1999 mat2000 mat2001
2   15      3   0.015   0.015   0.015   0.015
6   16      3   0.015   0.015   0.015   0.015
9   17      3   0.015   0.015   0.015   0.015
10  18      3   0.015   0.015   0.015   0.015
devtools::install_github("yikeshu0611/onetree")
library(onetree)
year <- rep(1998:2001, each = 4) #each was the change.
Age <- rep(15:18, 4)
mat1 <- rep(0.01, 16)
mat2 <- rep(0.012, 16)
mat3 <- rep(0.015, 16)
mat <- data.frame(year, Age, mat1, mat2, mat3)