mapply

assigning lists elements as function arguments

南笙酒味 提交于 2019-12-24 07:08:07
问题 Suppose that: list_a <- list(1, 10) list_2 <- list(5, 20) my.foo <- function (z,w) z+w My main question is: for each list_ object, how to pass its two elements as the arguments of my.foo so that to obtain 11 and 25? My closest guess to solve the problem so far is: mapply(my.foo, list_a, list_2) but it is not suited for what I need to do, as it returns 6 and 30. Thanks for any suggestions, Stefano 回答1: You can use ls and get to get the objects and do.call to call your function with the content

mapply for better performance

此生再无相见时 提交于 2019-12-24 05:57:28
问题 I want to apply a function to a matrix input a , this function would change the first element to c[a[1]] and the next elements to b[a[i],a[i+1]] starting from i = 1 up to i = ncol(a) - 1 . example input: a <- matrix(c(1,4,3,1),nrow=1) b <- matrix(1:25,ncol=5,nrow=5) c <- matrix(4:8,ncol=5,nrow=1) expected output: >a 4 16 14 3 #c[a[1]] gave us the first element: 4 #b[a[1],a[2]] gave us the second element: 16 #b[a[2],a[3]] gave us the third element: 14 #b[a[3],a[4]] gave us the fourth element:

Saving deeply nested files to specific directories with specific filenames

风格不统一 提交于 2019-12-23 04:35:07
问题 Given a 3 level nested list: mylist <- list("1000"=list("cars"=list("fast"=mtcars[1:10,], "slow"=mtcars[11:15,]), "flower"=iris), "2000"=list("tooth"=ToothGrowth, "air"=airquality, "cars"=list("cruiser"=mtcars[5:12,], "fast"=mtcars[1:3,], "mild"=mtcars[9:18,]))) (ie: mylist$1000$cars$fast , where fast is a dataframe, and cars and 1000 are nested lists in mylist ) I'd like to save each innermost dataframe, (ie: fast ) as a .csv with the df name as it's file name, ie: fast.csv , and I want the

Saving deeply nested files to specific directories with specific filenames

我与影子孤独终老i 提交于 2019-12-23 04:34:02
问题 Given a 3 level nested list: mylist <- list("1000"=list("cars"=list("fast"=mtcars[1:10,], "slow"=mtcars[11:15,]), "flower"=iris), "2000"=list("tooth"=ToothGrowth, "air"=airquality, "cars"=list("cruiser"=mtcars[5:12,], "fast"=mtcars[1:3,], "mild"=mtcars[9:18,]))) (ie: mylist$1000$cars$fast , where fast is a dataframe, and cars and 1000 are nested lists in mylist ) I'd like to save each innermost dataframe, (ie: fast ) as a .csv with the df name as it's file name, ie: fast.csv , and I want the

Count how many observations in the rest of the dat fits multiple conditions? (R)

流过昼夜 提交于 2019-12-23 04:26:39
问题 friends, I am new in R programming. I have been trying to write a user-defined function for days but not yet nailed it. This is a dataset called event, containing thousands of events (observations) and I selected several rows to show you the data structure. It contains the "STATEid," "date" of occurrence, and geographical coordinates in two variables "LON" "LAT." I am writing to calculate a new variable (column) for each row. This new variable should be: "Given any specific incident, count

How to concatenate data.frame inside lists by using names?

匆匆过客 提交于 2019-12-23 03:09:17
问题 I have to import over 1,000 excel files, and each excel contains multiple sheets (some have the same sheet name and some have different sheet names). Let's say with a small example as follows games <- data.frame(index = c(1,2,3), player = c('John', 'Sam', 'Mary')) weather <- data.frame(index = c(1,2,3), temperature = c('hot', 'cold', 'rainy')) cars <- data.frame(index = c(1,2,3), car = c('honda', 'toyota','bmw')) list1 <- list(games, weather, cars) names(list1) <- c('games', 'weather', 'cars'

How do I pass column-specific arguments to lapply in data.table .SD?

微笑、不失礼 提交于 2019-12-23 02:55:18
问题 I have seen examples of using .SD with lapply in data.table with a simple function as below: DT[ , .(b,d,e) := lapply(.SD, tan), .SDcols = .(b,d,e)] But I'm unsure of how to use column-specific arguments in a multiple argument function. For instance I have a winsorize function, I want to apply it to a subset of columns in a data table but using column-specific percentiles, e.g. library(DescTools) wlevel <- list(b=list(lower=0.01,upper=0.99), c=list(upper=0.02,upper=0.95)) DT[ , .(b,c) :

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

一笑奈何 提交于 2019-12-19 06:54:50
问题 (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()

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

六眼飞鱼酱① 提交于 2019-12-19 03:38:18
问题 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

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

老子叫甜甜 提交于 2019-12-17 21:03:55
问题 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