将列表元素作为数据帧传递,使用purrr和R应用函数和cbind

时间:2017-09-15 16:23:52

标签: r dataframe dplyr tidyverse purrr

我的数据如下:

test <- structure(
  list(
    sn = c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 
           11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 1640L, 1641L, 
           1642L, 1643L, 1644L, 1645L, 1646L, 1647L, 1648L, 1649L, 1650L, 
           1651L, 1652L, 1653L, 1654L, 1655L, 1656L, 1657L, 1658L, 1659L, 
           3279L, 3280L, 3281L, 3282L, 3283L, 3284L, 3285L, 3286L, 3287L, 
           3288L, 3289L, 3290L, 3291L, 3292L, 3293L, 3294L, 3295L, 3296L, 
           3297L, 3298L),
    END_hr = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
               1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 
               2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 
               3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
               3L, 3L, 3L),
    y_se = c(59401.8, 59328.9, 59259.5, 59190.7, 59121.7, 
             59044.4, 58969.6, 58901.8, 58842.9, 58770.6, 59556.1, 59489.7, 
             59428.8, 59369.2, 59309.3, 59243.7, 59180.2, 59122.5, 59069.9, 
             59008.2, 59401.8, 59328.9, 59259.5, 59190.7, 59121.7, 59044.4, 
             58969.6, 58901.8, 58842.9, 58770.6, 59556.1, 59489.7, 59428.8, 
             59369.2, 59309.3, 59243.7, 59180.2, 59122.5, 59069.9, 59008.2, 
             59401.8, 59328.9, 59259.5, 59190.7, 59121.7, 59044.4, 58969.6, 
             58901.8, 58842.9, 58770.6, 59556.1, 59489.7, 59428.8, 59369.2, 
             59309.3, 59243.7, 59180.2, 59122.5, 59069.9, 59008.2),
    y_sw = c(59182, 
             59108.3, 59037, 58966.1, 58894.5, 58812.6, 58733.1, 58661.1, 
             58599.1, 58520.7, 59401.8, 59328.9, 59259.5, 59190.7, 59121.7, 
             59044.4, 58969.6, 58901.8, 58842.9, 58770.6, 59182, 59108.3, 
             59037, 58966.1, 58894.5, 58812.6, 58733.1, 58661.1, 58599.1, 
             58520.7, 59401.8, 59328.9, 59259.5, 59190.7, 59121.7, 59044.4, 
             58969.6, 58901.8, 58842.9, 58770.6, 59182, 59108.3, 59037, 58966.1, 
             58894.5, 58812.6, 58733.1, 58661.1, 58599.1, 58520.7, 59401.8, 
             59328.9, 59259.5, 59190.7, 59121.7, 59044.4, 58969.6, 58901.8, 
             58842.9, 58770.6),
    y_nw = c(59108.3, 59037, 58966.1, 58894.5, 
             58812.6, 58733.1, 58661.1, 58599.1, 58520.7, 58449.2, 59328.9, 
             59259.5, 59190.7, 59121.7, 59044.4, 58969.6, 58901.8, 58842.9, 
             58770.6, 58708.1, 59108.3, 59037, 58966.1, 58894.5, 58812.6, 
             58733.1, 58661.1, 58599.1, 58520.7, 58449.2, 59328.9, 59259.5, 
             59190.7, 59121.7, 59044.4, 58969.6, 58901.8, 58842.9, 58770.6, 
             58708.1, 59108.3, 59037, 58966.1, 58894.5, 58812.6, 58733.1, 
             58661.1, 58599.1, 58520.7, 58449.2, 59328.9, 59259.5, 59190.7, 
             59121.7, 59044.4, 58969.6, 58901.8, 58842.9, 58770.6, 58708.1),
    y_ne = c(59328.9, 59259.5, 59190.7, 59121.7, 59044.4, 58969.6, 
             58901.8, 58842.9, 58770.6, 58708.1, 59489.7, 59428.8, 59369.2, 
             59309.3, 59243.7, 59180.2, 59122.5, 59069.9, 59008.2, 58958.8, 
             59328.9, 59259.5, 59190.7, 59121.7, 59044.4, 58969.6, 58901.8, 
             58842.9, 58770.6, 58708.1, 59489.7, 59428.8, 59369.2, 59309.3, 
             59243.7, 59180.2, 59122.5, 59069.9, 59008.2, 58958.8, 59328.9, 
             59259.5, 59190.7, 59121.7, 59044.4, 58969.6, 58901.8, 58842.9, 
             58770.6, 58708.1, 59489.7, 59428.8, 59369.2, 59309.3, 59243.7, 
             59180.2, 59122.5, 59069.9, 59008.2, 58958.8),
    x_se = c(299172.8, 
             299245.1, 299324.3, 299405.3, 299486.2, 299576.8, 299666, 299746.7, 
             299813.8, 299903.8, 299369.1, 299432.5, 299504.3, 299578.3, 299652.1, 
             299733.1, 299813.2, 299886.5, 299949.8, 300031.2, 299172.8, 299245.1, 
             299324.3, 299405.3, 299486.2, 299576.8, 299666, 299746.7, 299813.8, 
             299903.8, 299369.1, 299432.5, 299504.3, 299578.3, 299652.1, 299733.1, 
             299813.2, 299886.5, 299949.8, 300031.2, 299172.8, 299245.1, 299324.3, 
             299405.3, 299486.2, 299576.8, 299666, 299746.7, 299813.8, 299903.8, 
             299369.1, 299432.5, 299504.3, 299578.3, 299652.1, 299733.1, 299813.2, 
             299886.5, 299949.8, 300031.2),
    x_sw = c(298953.4, 299032.4, 299116.5, 
             299202.1, 299287.3, 299383.4, 299477.1, 299561, 299629.9, 299722.6, 
             299172.8, 299245.1, 299324.3, 299405.3, 299486.2, 299576.8, 299666, 
             299746.7, 299813.8, 299903.8, 298953.4, 299032.4, 299116.5, 299202.1, 
             299287.3, 299383.4, 299477.1, 299561, 299629.9, 299722.6, 299172.8, 
             299245.1, 299324.3, 299405.3, 299486.2, 299576.8, 299666, 299746.7, 
             299813.8, 299903.8, 298953.4, 299032.4, 299116.5, 299202.1, 299287.3, 
             299383.4, 299477.1, 299561, 299629.9, 299722.6, 299172.8, 299245.1, 
             299324.3, 299405.3, 299486.2, 299576.8, 299666, 299746.7, 299813.8, 
             299903.8),
    x_nw = c(299032.4, 299116.5, 299202.1, 299287.3, 299383.4, 
             299477.1, 299561, 299629.9, 299722.6, 299817.3, 299245.1, 299324.3, 
             299405.3, 299486.2, 299576.8, 299666, 299746.7, 299813.8, 299903.8, 
             299996.3, 299032.4, 299116.5, 299202.1, 299287.3, 299383.4, 299477.1, 
             299561, 299629.9, 299722.6, 299817.3, 299245.1, 299324.3, 299405.3, 
             299486.2, 299576.8, 299666, 299746.7, 299813.8, 299903.8, 299996.3, 
             299032.4, 299116.5, 299202.1, 299287.3, 299383.4, 299477.1, 299561, 
             299629.9, 299722.6, 299817.3, 299245.1, 299324.3, 299405.3, 299486.2, 
             299576.8, 299666, 299746.7, 299813.8, 299903.8, 299996.3),
    x_ne = c(299245.1, 
             299324.3, 299405.3, 299486.2, 299576.8, 299666, 299746.7, 299813.8, 
             299903.8, 299996.3, 299432.5, 299504.3, 299578.3, 299652.1, 299733.1, 
             299813.2, 299886.5, 299949.8, 300031.2, 300115.4, 299245.1, 299324.3, 
             299405.3, 299486.2, 299576.8, 299666, 299746.7, 299813.8, 299903.8, 
             299996.3, 299432.5, 299504.3, 299578.3, 299652.1, 299733.1, 299813.2, 
             299886.5, 299949.8, 300031.2, 300115.4, 299245.1, 299324.3, 299405.3, 
             299486.2, 299576.8, 299666, 299746.7, 299813.8, 299903.8, 299996.3, 
             299432.5, 299504.3, 299578.3, 299652.1, 299733.1, 299813.2, 299886.5, 
             299949.8, 300031.2, 300115.4),
    ans = c(31.2, 29.2, 9.01, 9.48, 
            10.3, 11.1, 11.1, 9.77, 29.3, 29.5, 32.2, 31.4, 28, 26.8, 30.9, 
            31.5, 31.6, 30.6, 31.9, 32.3, 30.1, 29.5, 24.5, 24.4, 26.5, 27, 
            27.1, 26.5, 30, 30.7, 29.3, 28.9, 28.5, 28.4, 29.3, 29.4, 29.4, 
            29.2, 29.2, 30.2, 29.1, 28.8, 28.5, 28.4, 28.4, 28.2, 28.1, 27.9, 
            28.2, 29.7, 29.1, 28.8, 28.7, 28.6, 28.4, 28.2, 28, 27.8, 27.9, 
            29)),
  .Names = c("sn", "END_hr", "y_se", "y_sw", "y_nw", "y_ne", 
             "x_se", "x_sw", "x_nw", "x_ne", "ans"),
  row.names = c(NA, -60L),
  class = "data.frame"
)

数据主管如下:

> head(test)
  sn END_hr    y_se    y_sw    y_nw    y_ne     x_se     x_sw     x_nw     x_ne   ans
1  1      1 59401.8 59182.0 59108.3 59328.9 299172.8 298953.4 299032.4 299245.1 31.20
2  2      1 59328.9 59108.3 59037.0 59259.5 299245.1 299032.4 299116.5 299324.3 29.20
3  3      1 59259.5 59037.0 58966.1 59190.7 299324.3 299116.5 299202.1 299405.3  9.01
4  4      1 59190.7 58966.1 58894.5 59121.7 299405.3 299202.1 299287.3 299486.2  9.48
5  5      1 59121.7 58894.5 58812.6 59044.4 299486.2 299287.3 299383.4 299576.8 10.30
6  6      1 59044.4 58812.6 58733.1 58969.6 299576.8 299383.4 299477.1 299666.0 11.10

现在,我想要做的是使用x和y的坐标以及ans参数的值绘制多边形。

八个坐标是多边形/四边形的四边的x和y坐标。我编写了一个函数,它从数据帧中获取值,然后它将创建一个允许绘制多边形的新数据框。

问题

我现在遇到的问题是如何将列表的元素作为dataframe传递给我的函数,处理它,然后使用rbind函数堆叠列表的每个元素的结果。

写入处理列表的每个元素的函数。我打算在以下函数中使用param = snparam1 = ans

datapoly <- function(df, param, param1){
  browser()
  a <- dim(df)
  ids <- as.character(df[,param])
  values <- data.frame(
    id = ids, 
    value = df[,param1]
  )

  positions <- data.frame(
    id = rep(ids, each = 4), 
    x = c(t(df[,c("x_se", "x_sw", "x_nw", "x_ne")])),
    y = c(t(df[,c("y_se","y_sw", "y_nw", "y_ne")]))
  )

  combine <- dplyr::left_join(x = values, y = positions, by = "id")
}

以下是我用于将test数据拆分为END_hr的代码。现在我想使用test %>% split(.$END_hr)的元素到我的函数datapoly

我尝试使用df = .,但它不起作用。

test %>% 
  split(.$END_hr) %>% 
  datapoly(df = ., param = "sn", param1 = "ans")

它可能比我采取的方法更简单。

解决此问题的一种方法如下:

test1 <- test %>% 
  split(.$END_hr)

datapoly(df = test1[[1]], param = "sn", param1 = "ans")

> datapoly(df = test1[[1]], param = "sn", param1 = "ans")
   id value        x       y
1   1 31.20 299172.8 59401.8
2   1 31.20 298953.4 59182.0
3   1 31.20 299032.4 59108.3
4   1 31.20 299245.1 59328.9
5   2 29.20 299245.1 59328.9
6   2 29.20 299032.4 59108.3
7   2 29.20 299116.5 59037.0
8   2 29.20 299324.3 59259.5
9   3  9.01 299324.3 59259.5
10  3  9.01 299116.5 59037.0
11  3  9.01 299202.1 58966.1
12  3  9.01 299405.3 59190.7
13  4  9.48 299405.3 59190.7
14  4  9.48 299202.1 58966.1
15  4  9.48 299287.3 58894.5
16  4  9.48 299486.2 59121.7
17  5 10.30 299486.2 59121.7
18  5 10.30 299287.3 58894.5
19  5 10.30 299383.4 58812.6
20  5 10.30 299576.8 59044.4
21  6 11.10 299576.8 59044.4
22  6 11.10 299383.4 58812.6
23  6 11.10 299477.1 58733.1
24  6 11.10 299666.0 58969.6
25  7 11.10 299666.0 58969.6
26  7 11.10 299477.1 58733.1
27  7 11.10 299561.0 58661.1
28  7 11.10 299746.7 58901.8
29  8  9.77 299746.7 58901.8
30  8  9.77 299561.0 58661.1
31  8  9.77 299629.9 58599.1
32  8  9.77 299813.8 58842.9
33  9 29.30 299813.8 58842.9
34  9 29.30 299629.9 58599.1
35  9 29.30 299722.6 58520.7
36  9 29.30 299903.8 58770.6
37 10 29.50 299903.8 58770.6
38 10 29.50 299722.6 58520.7
39 10 29.50 299817.3 58449.2
40 10 29.50 299996.3 58708.1
41 11 32.20 299369.1 59556.1
42 11 32.20 299172.8 59401.8
43 11 32.20 299245.1 59328.9
44 11 32.20 299432.5 59489.7
45 12 31.40 299432.5 59489.7
46 12 31.40 299245.1 59328.9
47 12 31.40 299324.3 59259.5
48 12 31.40 299504.3 59428.8
49 13 28.00 299504.3 59428.8
50 13 28.00 299324.3 59259.5
51 13 28.00 299405.3 59190.7
52 13 28.00 299578.3 59369.2
53 14 26.80 299578.3 59369.2
54 14 26.80 299405.3 59190.7
55 14 26.80 299486.2 59121.7
56 14 26.80 299652.1 59309.3
57 15 30.90 299652.1 59309.3
58 15 30.90 299486.2 59121.7
59 15 30.90 299576.8 59044.4
60 15 30.90 299733.1 59243.7
61 16 31.50 299733.1 59243.7
62 16 31.50 299576.8 59044.4
63 16 31.50 299666.0 58969.6
64 16 31.50 299813.2 59180.2
65 17 31.60 299813.2 59180.2
66 17 31.60 299666.0 58969.6
67 17 31.60 299746.7 58901.8
68 17 31.60 299886.5 59122.5
69 18 30.60 299886.5 59122.5
70 18 30.60 299746.7 58901.8
71 18 30.60 299813.8 58842.9
72 18 30.60 299949.8 59069.9
73 19 31.90 299949.8 59069.9
74 19 31.90 299813.8 58842.9
75 19 31.90 299903.8 58770.6
76 19 31.90 300031.2 59008.2
77 20 32.30 300031.2 59008.2
78 20 32.30 299903.8 58770.6
79 20 32.30 299996.3 58708.1
80 20 32.30 300115.4 58958.8

此简单数据的剩余两个元素的代码如下所示:

datapoly(df = test1[[2]], param = "sn", param1 = "ans")

datapoly(df = test1[[3]], param = "sn", param1 = "ans")

我有大量数据,我想使用purrr自动完成此过程。

0 个答案:

没有答案