How to perform an operation on a 3D array and a matrix whilst returning a 3D array?

In R, let’s have an array and a matrix: a <- array(rep(1:3, each = 4), dim = c(2, 2, 3)) b <- matrix(seq(0, 1, length.out = 6), nrow = 2, ncol = 3) I want to use an function that utilises matrices from a along the 3rd dimension and columns in b as its two… Read More How to perform an operation on a 3D array and a matrix whilst returning a 3D array?

apply or loop rbeta using different alpha and beta values from a dataframe in R

I’m trying to use sapply to create a matrix that contains output from rbeta using different specified alphas and betas obtained from a dataset. I think this should be fairly simple, but I am struggling. Here is some data q1 <- c(27, 104) q2 <- c(25, 121) q3 <- c(29, 114) df <- as.data.frame(rbind(q1,q2,q3)) colnames(df)… Read More apply or loop rbeta using different alpha and beta values from a dataframe in R

Create multiple objects resulting from a loop of paste(list.files)

I have multiple folders such as "results/[model_name_here]/rasters/" and I’m trying to create multiple objects resulting from a loop of list.files. For example: model_a <- list.files("results/rf/rasters", pattern = "tif$", full.names = TRUE) I was trying to go for a for loop such as models <- c("rf", "brt", "gam", "glm", "mars", "bart", "svm") for(i in models){ i… Read More Create multiple objects resulting from a loop of paste(list.files)

Web-Scraping using R (I want to extract some table like data from a website)

I’m having some problems scraping data from a website. I do have not a lot of experience with web-scraping. My intended plan is to scrape some data using R from the following website: https://www.myfxbook.com/forex-broker-swaps More precisely, I want to extract the Forex Brokers Swap Comparison for all the available pairs. My idea so far: library(XML)… Read More Web-Scraping using R (I want to extract some table like data from a website)

Converting maturities to numeric format

I am trying to make a function to replace weird looking maturities into numbers: For a reproducable example: the following block contains the data. dict <- c("ON","TN","1W","1M","2M","3M","6M","9M","1Y","1Y3M","1Y6M","1Y9M","2Y","2Y3M", "2Y6M","2Y9M","3Y","3Y3M","3Y6M","3Y9M","4Y","4Y3M","4Y6M","4Y9M","5Y","5Y3M","5Y6M","5Y9M","6Y","6Y3M","6Y6M","6Y9M","7Y","7Y3M","7Y6M","7Y9M","8Y","8Y3M","8Y6M","8Y9M","9Y","9Y3M","9Y6M","9Y9M","10Y","11Y","12Y","13Y","14Y","15Y","20Y","25Y","30Y","40Y","50Y") I tried to think about it as follows: make a sub() function for the months (extract everything before M and after Y). Divide by 12. Then add… Read More Converting maturities to numeric format

Select the first element from the first list, the second element from the second list, and so on, in a nested list

Let’s say I have a list like this: lst <- list(list(1,2,3),list(4,5,6),list(7,8,9)) I would then like to extract the elements 1, 5, and 9. How should I do that in an efficient manner? I’ve come across this post; Select first element of nested list, where it is suggested that one should use: lapply(x, ‘[[‘, 1) to… Read More Select the first element from the first list, the second element from the second list, and so on, in a nested list

Vectorised argument for a function in R. The function gives out multiple data frames, whereas I'd like it to output only one

I’d like to compute trimmed mean for each trimming proportion alpha, and then see which trimming proportion gives the minimal variance of the trimmed means, when Bootstrap simulations of size N=200 are applied. The problem that I have, is that when I try to create a data frame of column1 = mean and column2 =… Read More Vectorised argument for a function in R. The function gives out multiple data frames, whereas I'd like it to output only one