mapply

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

ⅰ亾dé卋堺 提交于 2019-12-02 16:43:29
问题 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 to perform: # the table dt1 <- data.table(parkName=rep(c("Zone A","Zone B", "Zone C" , "Zone D"),5), boundary=rep(0:1,10),v=1:20, w=rnorm(20))[] # criteria for subsetting the table dt2 <- data.table(zone1 = c("Zone A","Zone B"), zone2 =c("Zone B",

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

拟墨画扇 提交于 2019-12-02 09:03:06
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 to perform: # the table dt1 <- data.table(parkName=rep(c("Zone A","Zone B", "Zone C" , "Zone D"),5), boundary=rep(0:1,10),v=1:20, w=rnorm(20))[] # criteria for subsetting the table dt2 <- data.table(zone1 = c("Zone A","Zone B"), zone2 =c("Zone B","Zone C")) # function for subsetting the table and plotting p <- function(sd1,sd2){ dlist <- dt1[parkName=

R: How to run a function/calculation on two lists based on their names?

坚强是说给别人听的谎言 提交于 2019-12-02 08:30:55
I want to run a function (in this case just a multiplication) on two list based on their names. Here some example data showing my structure: A <- list("111"=matrix(sample(1:10,9), nrow=3, ncol=3), "112"=matrix(sample(1:10,9), nrow=3, ncol=3)) names <- list(c("A", "B", "C"), c("A", "B", "C")) A <- lapply(ProdValues, function (x) {dimnames(x) <- names; return(x)}) List A has values in matrices for different products (listnames=111,112) and List B (below) has YEARLY values for the same products, names are composed of product and year and separated by "." (e.g. 111.2000): B <- list("111.2000"

Environments in R, mapply and get

﹥>﹥吖頭↗ 提交于 2019-12-02 02:57:08
Let x<-2 in the global env: x <-2 x [1] 2 Let a be a function that defines another x locally and uses get : a<-function(){ x<-1 get("x") } This function correctly gets x from the local enviroment: a() [1] 1 Now let's define a function b as below, that uses mapply with get : b<-function(){ x<-1 mapply(get,"x") } If I call b , it seems that mapply makes get not search the function environment first. Instead, it tries to get x directly form the global enviroment, and if x is not defined in the global env, it gives an error message: b() x 2 rm(x) b() Error in (function (x, pos = -1L, envir = as

Row-wise sort then concatenate across specific columns of data frame

强颜欢笑 提交于 2019-12-01 06:06:21
(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. So for example, in row 5, if item1 = "milk", item2 = "eggs", and item3 = "butter", the new variable in row 5 might be "butter, eggs, milk" I wrote a function f() below that works on two character variables. However, I am having trouble Using mapply or other

How to combine rapply() and mapply(), or how to use mapply/Map recursively?

自闭症网瘾萝莉.ら 提交于 2019-11-30 22:04:23
I was wondering if there's a simple way to combine the functions of rapply( , how = "replace") and mapply() , in order to use mapply() on nested lists recursively. For instance, I have two nested lists: A = list(list(c(1,2,3), c(2,3,4)), list(c(4,3,2), c(3,2,1))) B = list(list(c(1,2,3), c(2,3,4)), list(c(4,3,2), c(3,2,1))) Let's say I want to apply function(x, y) x + y to all the corresponding elements in A and B and preserve the nested structure. The desired result would be result = list(list(c(2,4,6), c(4,6,8)), list(c(8,6,4), c(6,4,2))) I think this should be a mapply() analog of rapply(x,

R: apply a function to every element of two variables respectively

旧城冷巷雨未停 提交于 2019-11-29 19:56:30
问题 I have a function with two variables x and y: fun1 <- function(x,y) { z <- x+y return(z) } The function work fine by itself: fun1(15,20) But when I try to use it with two vectors for x and y with an apply function I do not get the correct 56*121 array Lx <- c(1:56) Ly <- c(1:121) mapply(fun1, Lx, Ly) I would be grateful for your help and also on advice on the fastest solution (eg is a data.table or dplyr solution faster than apply). 回答1: If you want to use mapply() you have to provide it with

Vectorize() vs apply()

和自甴很熟 提交于 2019-11-29 01:45:57
The Vectorize() and the apply() functions in R can often be used to accomplish the same goal. I usually prefer vectorizing a function for readability reasons, because the main calling function is related to the task at hand while sapply is not. It is also useful to Vectorize() when I am going to be using that vectorized function multiple times in my R code. For instance: a <- 100 b <- 200 c <- 300 varnames <- c('a', 'b', 'c') getv <- Vectorize(get) getv(varnames) vs sapply(varnames, get) However, at least on SO I rarely see examples with Vectorize() in the solution, only apply() (or one of it

mapply basics? - how to create a matrix from two vectors and a function

橙三吉。 提交于 2019-11-28 13:36:24
I am trying create a data.frame from which to create a graph. I have a function and two vectors that I want to use as the two inputs. This is a bit simplified, but basically all I have is: relGPA <- seq(-1.5,1.5,.2) avgGPA <- c(-2,0,2) f <- function(relGPA, avgGPA) 1/(1+exp(sum(relGPA*pred.model$coef[1],avgGPA*pred.model$coef[2]))) and all I want is a data.frame with 3 columns for the avgGPA values, and 16 rows for the relGPA values with the resulting values in the cells. I apologize for how basic this is, but I assure you I have tried to make this happen without your assistance. I have tried

Force mapply to return a list?

拈花ヽ惹草 提交于 2019-11-27 08:53:45
Suppose I have a function that creates data frames. I'd like to run that function with different input values, and then rbind the results together into one big data frame, as below: CreateDataFrame <- function(type="A", n=10, n.true=8) { data.frame(success=c(rep(TRUE, n.true), rep(FALSE, n - n.true)), type=type) } df <- do.call(rbind, lapply(toupper(letters[1:5]), CreateDataFrame)) My CreateDataFrame function takes three arguments. In the example above, the second and third arguments are held constant. I'd like to do the same as above, but have the second and third arguments change on each