首页 文章

R - 将函数应用于数据帧的每一行,函数的参数是每行的值

提问于
浏览
0

我想从数据帧的每一行的某些列中获取值,并将它们用作函数的参数,以生成预测值的向量(每行一个) .

见下面的功能:

predicted_value_generator <- function(theta, tau_host, tau_targ, rho, pop_host, pop_targ, distance, R0){
return(theta * R0 * (pop_host^tau_host) * (pop_targ^tau_targ) * distance^(-rho))
}

Dataframes:

> pop_dist_data

            X time_to_spread host_city_pop target_city_pop    distance      host_city_outgoing_flights target_city_incoming_flights flights_between      Reproduction_number_R0  
4           3              2        198100          622104  460.819668                            9158                        93861               6                       1.365
5           4              2        198100          622104  460.819668                            9158                        93861               6                       1.300
6           5              1        198100          622104  460.819668                            9158                        93861               6                       1.300
7           6              2        198100          622104  460.819668                            9158                        93861               6                       1.300
8           7              1        198100          622104  460.819668                            9158                        93861               6                       1.300

> fit_data
  X     theta1     tau_host  tau_targ        rho
1 0 0.05447868 3.288922e-09 0.1491428 0.00820936

目前的做法:

pred_vals <- apply(pop_dist_data, 1, predicted_value_generator(fit_data[3], fit_data[4], fit_data[5], fit_data[6], pop_dist_data[,3], pop_dist_data[,4], pop_dist_data[,5], pop_dist_data[,9]))

预期的产出是这样的:

> pred_vals
[1] 0.43 0.96 0.33 0.5 0.27

无论行如何, fit_data 参数都是常量,但 pop_dist_data 参数应该在每个特定行使用该列值 .

最有效的方法是什么?

先感谢您 .

1 回答

  • 1

    这里有几个问题:

    • dat[:, j] 无效R语法 . 使用 dat[, j]

    • R0 无处可定

    • predicted_value_generator 已向量化,因此我们不需要 apply

    • pop_dist_data 没有9列

    对于SO的

    • 问题应该以可重现的方式显示,以便人们可以简单地复制并粘贴问题中的代码和数据 . 这可以通过为每个输入 X 显示 dput(X) 的输出来完成 . 我在最后的笔记中已经这样做了 .

    假设缺少 R0 已定义,请尝试将两个数据框绑定在一起,然后只调用具有相应列名的函数 .

    with(cbind(pop_dist_data, fit_data), 
           predicted_value_generator(theta1, tau_host, tau_targ, rho, 
                                     host_city_pop, target_city_pop, distance, R0))
    

    注意

    可重复形式的输入:

    pop_dist_data <-
    structure(list(X = 3:7, time_to_spread = c(2L, 2L, 1L, 2L, 1L
    ), host_city_pop = c(198100L, 198100L, 198100L, 198100L, 198100L
    ), target_city_pop = c(622104L, 622104L, 622104L, 622104L, 622104L
    ), distance = c(460.819668, 460.819668, 460.819668, 460.819668, 
    460.819668)), .Names = c("X", "time_to_spread", "host_city_pop", 
    "target_city_pop", "distance"), class = "data.frame", row.names = c("4", 
    "5", "6", "7", "8"))
    
    fit_data <- 
    structure(list(X = 0L, theta1 = 0.05447868, tau_host = 3.288922e-09, 
        tau_targ = 0.1491428, rho = 0.00820936), .Names = c("X", 
    "theta1", "tau_host", "tau_targ", "rho"), class = "data.frame", row.names = "1")
    

相关问题