FAQ Database Discussion Community


Speeding up a function involving mapply and integrate

r,optimization,integrate,mapply
I've inherited R some code and it runs incredibly slowly. Most of the time is spent evaluating the functions of the form (there are about 15 such functions with different integrands G): TMin <- 0.5 F <- function (t, d) { result <- ifelse(((d > 0) & (t > TMin)),...

Remove NAs when using mapply for ttest in R

r,mapply,na.rm
I would like to do a column-wise ttest between two dataframes in R. That is, ttest(df1$col1,df2$col1) ,ttest(df1$col2,df2$col2) and so on....The best option here is to use mapply or Map function. Something like: mapply(t.test,tnav_DJF_histo.csv[,-1],tnav_DJF.csv[,-1]) works perfectly but if one of your df columns has NAs, it fails with this error: Error...

R row-wise sort then concatenate across specific columns of data frame

r,sorting,row,string-concatenation,mapply
(Related question that does not include sorting. It's easy to just use paste when you don't need to sort.) I have a less-than-ideally-structured table with character columns that are generic "item1","item2" etc. I would like to create a new character variable that is the alphabetized, comma-separated concatenation of these columns....

R: Shiny - Subset data based on selections from checkboxGroupInput"

r,checkbox,shiny,mapply
I've a data frame with 3 columns: Mes, Visitas, Pedidos. Code: structure(list(Mes = structure(1:12, .Label = c("Enero", "Febrero", "Marzo", "Abril", "Mayo", "Junio", "Julio", "Agosto", "Septiembre", "Octubre", "Noviembre", "Diciembre"), class = "factor"), Visitas = c(100L, 200L, 300L, 400L, 500L, 600L, 700L, 800L, 900L, 1000L, 1100L, 1200L), Pedidos = c(20L, 40L, 60L,...

Apply a list of n *expressions* to each row of a dataframe?

r,apply,lapply,mapply
In short, I have a list of expressions that I want to apply to each row of a dataframe. This is very similar to this question, but there is a subtle difference in that I do not have a list of functions, but have a list of expressions. Here's what...

Using Map with mget yields unexpected output

r,mapply
I have a number of lists :say list1, list2,....and list 100 and function myfun . With these, I can run Map(myfun,list1,list2,...,list100). However, I want to avoid writing all list numbers, and so I tried Map(myfun,mget(paste0("list",1:100))). But, this didn't work. A reproducible example with only three lists is provided below: x1<-list(1:10,10:20)...

Using lapply to list percentage of null variables in every column in R

r,apply,lapply,mapply
I was given a large csv that is 115 columns across and 1000 rows. The columns have a variety of data, some is character-based, some is integer, etc. However, the data has a LOT of null variables of varying types (NA, -999, NULL, etc.). What I want to do is...

recursive program in r

r,recursion,mapply
I am doing some computations, but am having a hard time wrining a program in r that accomplishes what I need. x1<-c('a','b','c','d','a') x2<-c('b','e','g') x3<-c('c','a','h','j') x4<-c('d','l','m','o','p','x','y','z') x5<-c('f','q','a') I am looking of a way to compute y1<-length(intersect(x1,x2)) y2<-length(intersect(x3, union(x1,x2))) y3<-length(intersect(x4, union(x3,union(x1,x2)))) y4<-length(intersect(x5, union(x4, union(x3,union(x1,x2))))) ...

How to generalize mapply to work “crosswisely”?

r,function,vectorization,apply,mapply
Mapply applies a 2-dimensional function to the 1st elements of each m-dimensional vector, and then to the 2nd elements of each, etc. The result is an m-dimensional vector. For example > mapply(sum, 1:5, 12:16) [1] 13 15 17 19 21 Now, is there a DIRECT alternative to mapply that applies...

Using match.call() with mapply

r,mapply
I have a function that basically outputs a boolean condition as a string from the arguments (the details of the function don't matter here) makeClause <-function(Sex=c("NA", "male", "female"), SmokingHx=c("NA", "current", "former", "never"), conjunction=c("&", "|")) { arglist = as.list(match.call()) return(arglist) } I have a data frame that has all combinations of...

R sub select from a list of vectors using do.call, lapply or mapply?

r,lapply,mapply,do.call
I have the following data. x is a list of vectors and indices is a list of indices. x = list(c("a", "b", "c", "a"), c("b", "x", "a", "c")) indices = list(c(1, 2), c(3, 4)) What I want to do is step through each vector represented within the list x and...

Making multiple plots with mapply

r,ggplot2,mapply
I made the following dataframe df: V1 <- 1:10 V2 <- 11:20 V3 <- 21:30 V4 <- 31:40 df <- data.frame(V1,V2,V3,V4) I also made a function which should make a simple scatterplot based on the arguments var1 and var2. ScatterPlot <- function(var1, var2) { ggplot(data = df, aes(x = var1,...

R, mapply , ggplot : EXPR must be a length 1 vector

r,ggplot2,gridextra,mapply
I am trying plot the subsets of a table using ggplot and gridExtra. But I have bumbed in the following error EXPR must be a length 1 vector. I could come up with any side step. Any help would be useful. Here is a little example of what I'm trying...