在函数中包含dplyr

时间:2018-10-02 08:22:51

标签: r dplyr

<dependency>
    <groupId>org.postgresql</groupId>
    <artifactId>postgresql</artifactId>
    <version>42.2.2</version>
</dependency>

我尝试编写一个包含df99 <- structure(list(Yta = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), name = c("Lettland", "Lettland", "Lettland", "Lettland", "Lettland", "Lettland", "Lettland", "Lettland", "Lettland", "Lettland", "Lettland", "Lettland", "Lettland", "Lettland", "Lettland", "Lettland", "Lettland", "Lettland", "Lettland", "Lettland", "Lettland", "Lettland", "Lettland", "Lettland", "Lettland", "Lettland", "Lettland", "Lettland", "Lettland", "Lettland", "Lettland", "Lettland", "Lettland", "Lettland", "Lettland"), H99 = c(92L, 87L, 88L, 89L, 98L, 88L, 88L, 87L, 85L, 93L, 87L, 83L, 92L, 89L, 94L, 98L, 93L, 97L, 93L, 88L, 94L, 93L, 98L, 95L, 91L, 95L, 96L, 91L, 93L, 92L, 91L, 84L, 87L, 96L, 94L), D99 = c(75L, 72L, 75L, 80L, 79L, 82L, 78L, 75L, 62L, 83L, 70L, 81L, 80L, 89L, 88L, 95L, 86L, 76L, 76L, 67L, 81L, 85L, 91L, 85L, 69L, 67L, 85L, 79L, 80L, 79L, 83L, 58L, 56L, 111L, 74L)), class = "data.frame", row.names = c(NA, -35L ), .Names = c("Yta", "name", "H99", "D99")) 包的函数

dplyr

但是出了点问题

  

quo_d ^ 2中的错误:二进制运算符的非数字参数

1 个答案:

答案 0 :(得分:1)

我们可以使用paste

创建公式
library(tidyverse)
library(broom)
calcCoef <- function(dff, h, d){
    quo_h <- enquo(h) 
    quo_d <- enquo(d)

    qn_h <- quo_name(quo_h)
    qn_d <- quo_name(quo_d)
    fml <- as.formula(paste0(qn_h, " ~ ", qn_d, "/ (a + b * ", qn_d, ")^2 + 1.3"))


    dff %>% 
        group_by(Yta) %>% 
        filter(!! quo_d > 0) %>%
        do(tidy(nls(fml, start = c(a = 0.1, b = 0.1), data =  .))) %>%
        select(Yta, term, estimate) %>%
        spread(term, estimate)


}


calcCoef(df99, h = H99, d = D99)
# A tibble: 1 x 3
# Groups:   Yta [1]
#    Yta      a        b
#  <int>  <dbl>    <dbl>
#1     1 -0.554 -0.00480