R - 将 3 参数函数应用于 3 列矩阵的每一行,使用 3 列的每个值作为参数
R - Apply a 3 arguments function to each line of a 3 columns matrix, using as arguments each value of the 3 columns
我有一个函数 getSentiment(来自“edgar”R 包),它为我提供了一个带有一组度量的数据框,其工作方式如下:
getSentiment(cik.no = "cik_number", form.type = "form_type", filing_year = year)
该函数不适用于矢量,所以我无法使用 C("cik_number1", "cik_number2") 检查多个 cik 号码,表格类型和申请年份也是如此.
在我这边,我有一个包含 700 多家公司的矩阵,其中有 3 列,一列用于 cik 编号,一列用于表格类型,最后一列用于申请年份。
我想要的是将 getSentiment 函数应用于矩阵的每个 line/observation,将 3 列中每行的对应值作为参数。
然后,为了完成它,我想将我为每家公司获得的所有数据帧按照它们执行的顺序绑定到一个大矩阵中。
也许解决方法很简单,我是五月份开始学R的,但是一直学不会。
如果有人能帮助我,那就太好了,因为我正在做的是我的硕士论文。
谢谢
假设您的数据名为 data
并且列的顺序为 cik.no
、form.type
和 filing_year
您可以尝试以下操作。
result <- do.call(rbind, apply(data, 1, function(x)
getSentiment(cik.no = x[1], form.type =x[2], filing_year = x[3])))
同样,这个使用 Map
的解决方案也应该有效。
result <- do.call(rbind, Map(getSentiment, data[[1]], data[[2]], data[[3]]))
如果您使用矩阵行的索引生成向量,则可以将其用作 purrr
包中函数 map_dfr
的输入。此函数将提供的函数应用于提供的向量的每个元素(这里是您的行索引)并将结果 data.frame.
绑定在一起
test_mat <- matrix(1:9, ncol = 3)
test_fun <- function(a, b, c) {
data.frame(c1 = a,
c2 = 2 * b,
c3 = 3 * c)
}
number_row <- seq_len(nrow(test_mat))
res <- purrr::map_dfr(number_row, ~test_fun(test_mat[.x, 1],
test_mat[.x, 2],
test_mat[.x, 3]))
res
#> c1 c2 c3
#> 1 1 8 21
#> 2 2 10 24
#> 3 3 12 27
由 reprex package (v0.3.0)
于 2020-09-19 创建
对于您的函数,将其更改为:
res <- purrr::map_dfr(number_row, ~getSentiment(cik.no[.x, 1],
form.type[.x, 2],
filing_year[.x, 3]))
(假设你的数据中cik.no
、form.type
和filing_year
的顺序)
您可以使用 tidyverse purrr
包中的 pmap
函数。输出将是您函数中 return 矩阵的列表。然后你可以 rbind
一起输出:
library(tidyverse)
paramlist <- list(cik.no, form.type, filing.year) # the parameters are vectors
outputs <- pmap(paramlist, getSentiment)
final <- do.call(rbind, outputs)
我有一个函数 getSentiment(来自“edgar”R 包),它为我提供了一个带有一组度量的数据框,其工作方式如下:
getSentiment(cik.no = "cik_number", form.type = "form_type", filing_year = year)
该函数不适用于矢量,所以我无法使用 C("cik_number1", "cik_number2") 检查多个 cik 号码,表格类型和申请年份也是如此. 在我这边,我有一个包含 700 多家公司的矩阵,其中有 3 列,一列用于 cik 编号,一列用于表格类型,最后一列用于申请年份。 我想要的是将 getSentiment 函数应用于矩阵的每个 line/observation,将 3 列中每行的对应值作为参数。 然后,为了完成它,我想将我为每家公司获得的所有数据帧按照它们执行的顺序绑定到一个大矩阵中。
也许解决方法很简单,我是五月份开始学R的,但是一直学不会。 如果有人能帮助我,那就太好了,因为我正在做的是我的硕士论文。
谢谢
假设您的数据名为 data
并且列的顺序为 cik.no
、form.type
和 filing_year
您可以尝试以下操作。
result <- do.call(rbind, apply(data, 1, function(x)
getSentiment(cik.no = x[1], form.type =x[2], filing_year = x[3])))
同样,这个使用 Map
的解决方案也应该有效。
result <- do.call(rbind, Map(getSentiment, data[[1]], data[[2]], data[[3]]))
如果您使用矩阵行的索引生成向量,则可以将其用作 purrr
包中函数 map_dfr
的输入。此函数将提供的函数应用于提供的向量的每个元素(这里是您的行索引)并将结果 data.frame.
test_mat <- matrix(1:9, ncol = 3)
test_fun <- function(a, b, c) {
data.frame(c1 = a,
c2 = 2 * b,
c3 = 3 * c)
}
number_row <- seq_len(nrow(test_mat))
res <- purrr::map_dfr(number_row, ~test_fun(test_mat[.x, 1],
test_mat[.x, 2],
test_mat[.x, 3]))
res
#> c1 c2 c3
#> 1 1 8 21
#> 2 2 10 24
#> 3 3 12 27
由 reprex package (v0.3.0)
于 2020-09-19 创建对于您的函数,将其更改为:
res <- purrr::map_dfr(number_row, ~getSentiment(cik.no[.x, 1],
form.type[.x, 2],
filing_year[.x, 3]))
(假设你的数据中cik.no
、form.type
和filing_year
的顺序)
您可以使用 tidyverse purrr
包中的 pmap
函数。输出将是您函数中 return 矩阵的列表。然后你可以 rbind
一起输出:
library(tidyverse)
paramlist <- list(cik.no, form.type, filing.year) # the parameters are vectors
outputs <- pmap(paramlist, getSentiment)
final <- do.call(rbind, outputs)