# R apply with a vector parameter

I am trying to apply the ppoibin (poisson binomial probabilities) function from the poibin package to each row in a dataframe. This function takes two parameters: an integer and a vector of probabilities. My data takes the form: p <- matrix(c(0.046, 0.046, 0.323, 0.122, 0.122, 0.490, 0.122, 0.122, 0.490), 3 , 3, byrow = TRUE)… Read More R apply with a vector parameter

# Using unlist on a column of strings within a data frame

I have a data frame with a column that contains a string with multiple names separated by commas: df = data.frame(my.text = c("John Smith, Johnny Smith, John Smith", "John Doe, Doe, Johnny", c="Jane Doe, Jane Doe")) df my.text 1 John Smith, Johnny Smith, John Smith 2 John Doe, Doe, Johnny 3 Jane Doe, Jane Doe… Read More Using unlist on a column of strings within a data frame

# Applying custom function to a list of DFs, taking another list as an input – R

I have a list of dfs and a list of annual budgets. Each df represents one business year, and each budget represents a total spend for that year. # the business year starts from Feb and ends in Jan. # the budget column is first populated with the % of annual budget allocation df <-… Read More Applying custom function to a list of DFs, taking another list as an input – R

# mapply for a single list

I understand the usage of mapply(FUN, …) that cycles through elements of each of the … arguments and applies FUN to those combinations. What if instead of multiple arguments in … I have them in one list? Example use case: I define a function that pastes arguments excluding NA: pasteNotNA <- function(…,collapse=’, ‘){ dots <-… Read More mapply for a single list

# Fast fill 3D array/matrix in R with known index and value

may I ask a stupid question about fast assigning values to a big 3D matrix in R? I have x, y, z for location index and val for values to fill. The for loop takes so long, could you please help? Note that here I just simulated xyz and val x <- replicate(1e4, sample(seq(1:100), size… Read More Fast fill 3D array/matrix in R with known index and value

# R: using mapply for a function of two vectors

I have an R function that calculates the Hamming distance of two vectors: Hamming = function(x,y){ get_dist = sum(x != y, na.rm=TRUE) return(get_dist) } that I would like to apply to every row of two matrices M1, M2 without using a for loop. What I currently have (where L is the number of rows in… Read More R: using mapply for a function of two vectors