简体   繁体   English

Io如何将我自己的function应用于矩阵?

[英]How do Io apply my own function to matrices?

I am a newbie in R and I am learning how to apply my own function to matrices.我是 R 的新手,我正在学习如何将我自己的 function 应用于矩阵。 I wrote the following code:我写了以下代码:

HAR_ReV<-function(vec){
  name<-rlang::enexpr(vec)
  x<-vec
  ReV_d = ReV_w = ReV_m = NULL

  TT = length(x)
  for (i in 31:TT){
    ReV_d[i] = x[(i-1)]
    ReV_w[i] = sum(x[(i-7):(i-1)],na.rm = T)/7 
    ReV_m[i] = sum(x[(i-30):(i-1)],na.rm = T)/30 
  }
  Realized_Variances = cbind(ReV_d, ReV_w, ReV_m)
  colnames(Realized_Variances)= c(paste("ReV_d",name, sep = "_"), 
                                  paste("ReV_w",name, sep = "_"),
                                  paste("ReV_m",name, sep = "_"))
  Realized_Variances
}

The code was made for being applied in vectors and the function above create a new matrix, size 3530 (including NA's) X 3, for each vector.该代码是为应用于向量而编写的,上面的 function 为每个向量创建了一个新矩阵,大小为 3530(包括 NA)X 3。 Now, I have a matrix that contains all vectors and I want to apply this function in order to obtain a single matrix size 3530 x 36.现在,我有一个包含所有向量的矩阵,我想应用这个 function 以获得单个矩阵大小 3530 x 36。

My Matrix has the following structure:我的矩阵具有以下结构:

Matrix<-matrix(1:45, nrow = 3530,   ncol = 12)
dimnames(Matrix) <- list(NULL, NULL)
dimnames(Matrix) <- list(c(1:3530), c("rrp_nsw_d", "rrp_qld_d"  , "rrp_sa_d", "rrp_vic_d",     "rrp_nsw_RV_pos", "rrp_qld_RV_pos", "rrp_sa_RV_pos", "rrp_vic_RV_pos", 
                      "rrp_nsw_RV_neg" , "rrp_qld_RV_neg", "rrp_sa_RV_neg", "rrp_vic_RV_neg"))

Thanks for your help谢谢你的帮助

Consider lapply to build a list of matrices that you can then cbind together at the end.考虑lapply构建一个矩阵列表,然后您可以在最后将其cbind在一起。 However, to do so, a slight adjustment is needed.然而,要做到这一点,需要稍作调整。 Instead of defining name with rlang::enexpr that takes literal argument name, consider passing name as a parameter such as the original matrix column number.与其使用采用文字参数名称的rlang::enexpr定义名称,不如考虑将名称作为参数传递,例如原始矩阵列号。 Also, define your vectors length in advance instead of growing them in a loop:此外,提前定义向量长度,而不是在循环中增长它们:

Adjusted Function调整Function

HAR_ReV <- function(vec, name){
  #name <- rlang::enexpr(vec)          # REMOVE LINE
  x <- vec
  TT <- length(x)
  # DEFINE VECTOR LENGTH IN ADVANCE (MORE EFFICIENT THAN GROWING THEM IN LOOP)
  ReV_d <- ReV_w <- ReV_m <- vector(mode="numeric", length=TT-30)

  for (i in 31:TT){
    ReV_d[i] <- x[(i-1)]
    ReV_w[i] <- sum(x[(i-7):(i-1)],na.rm = T)/7 
    ReV_m[i] <- sum(x[(i-30):(i-1)],na.rm = T)/30 
  }
  Realized_Variances <- cbind(ReV_d, ReV_w, ReV_m)
  colnames(Realized_Variances) <- c(paste("ReV_d", name, sep = "_"), 
                                    paste("ReV_w", name, sep = "_"),
                                    paste("ReV_m", name, sep = "_"))
  return(Realized_Variances)
}

Matrix Build矩阵构建

my_matrix <- matrix(1:45, nrow = 3530, ncol = 12,
                    dimnames = list(c(1:3530), 
                                    c("rrp_nsw_d", "rrp_qld_d"  , "rrp_sa_d", "rrp_vic_d",     
                                      "rrp_nsw_RV_pos", "rrp_qld_RV_pos", "rrp_sa_RV_pos", 
                                      "rrp_vic_RV_pos", "rrp_nsw_RV_neg" , "rrp_qld_RV_neg", 
                                      "rrp_sa_RV_neg", "rrp_vic_RV_neg")
                    )
)

# LIST OF MATRICES
final_matrix_list <- Map(function(i,n) HAR_ReV(my_matrix[,i], n), 
                         1:ncol(my_matrix), colnames(my_matrix))

# FINAL MATRIX
final_matrix <- do.call(cbind, final_matrix_list)

dim(final_matrix)
# [1] 3530   36

str(final_matrix)
# num [1:3530, 1:36] 0 0 0 0 0 0 0 0 0 0 ...
# - attr(*, "dimnames")=List of 2
# ..$ : NULL
# ..$ : chr [1:36] "ReV_d_rrp_nsw_d" "ReV_w_rrp_nsw_d" "ReV_m_rrp_nsw_d" "ReV_d_rrp_qld_d" ...

str(final_matrix)

The following will give you the result you are looking for:以下将为您提供您正在寻找的结果:

result <- as.matrix(do.call("cbind", lapply(as.data.frame(Matrix), 
                                            function(x) {
                                              y <- as.integer(x); 
                                              HAR_ReV(y)
                                            })))

Obviously I can't fit the result in here, but you can check out the dimensions of the result:显然我不能把结果放在这里,但你可以检查结果的尺寸:

dim(result)
#> [1] 3530   36

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM