lapply

How to get the name of a data.frame within a list?

走远了吗. 提交于 2019-11-30 04:20:03
问题 How can I get a data frame's name from a list? Sure, get() gets the object itself, but I want to have its name for use within another function. Here's the use case, in case you would rather suggest a work around: lapply(somelistOfDataframes, function(X) { ddply(X, .(idx, bynameofX), summarise, checkSum = sum(value)) }) There is a column in each data frame that goes by the same name as the data frame within the list. How can I get this name bynameofX ? names(X) would return the whole vector.

How to apply mean function over elements of a list in R

假装没事ソ 提交于 2019-11-30 03:15:10
问题 I have a list and I want to use lapply() to compute the mean of its elements. For example, for the seventh item of the list I have: >list[[7]] [1] 1 1 1 1 1 1 1 1 1 1 and my output should be: > mean(temp[[7]][1:10]) [1] 1 But when I use lapply() like below the result would be something else. What should I do? > lapply(list[[7]][1:10],mean) [[1]] [1] 1 [[2]] [1] 1 . . . [[10]] [1] 1 回答1: To get the mean of the 7th element of the list just use mean(list[[7]]) . To get the mean of each element

Creating a named list from two vectors (names, values)

孤者浪人 提交于 2019-11-30 01:18:08
Is there a way to use mapply on two vectors to construct a named list? The first vector would be of type character and contain the names used for the list while the second contains the values. So far, the only solution I have is: > dummyList = list() > addToList <- function(name, value) { + dummyList[[name]] <- value + } > mapply(addToList, c("foo", "bar"), as.list(c(1, 2)) $foo `1` $bar `2` This seems like a rather contrived solution, but I can't figure out how to do it otherwise. The problems I have with it are: It requires the creation of dummyList even though dummyList is never changed and

Apply function on a subset of columns (.SDcols) whilst applying a different function on another column (within groups)

我的梦境 提交于 2019-11-29 20:26:11
This is very similar to a question applying a common function to multiple columns of a data.table uning .SDcols answered thoroughly here . The difference is that I would like to simultaneously apply a different function on another column which is not part of the .SD subset. I post a simple example below to show my attempt to solve the problem: dt = data.table(grp = sample(letters[1:3],100, replace = TRUE), v1 = rnorm(100), v2 = rnorm(100), v3 = rnorm(100)) sd.cols = c("v2", "v3") dt.out = dt[, list(v1 = sum(v1), lapply(.SD,mean)), by = grp, .SDcols = sd.cols] Yields the following error: Error

How to subset data.frames stored in a list?

十年热恋 提交于 2019-11-29 15:12:52
问题 I created a list and I stored one data frame in each component. Now I would like to filter those data frames keeping only the rows that have NA in a specific column. I would like the result of this operation to be another list containing data frames with only those rows having NA in that column. Here is some code to clarify what I am saying. Assume d1 and d2 are my data frames set.seed(1) d1<-data.frame(a=rnorm(5), b=c(rep(2006, times=4),NA)) d2<-data.frame(a=1:5, b=c(2007, 2007, NA, NA, 2007

Fast alternative to split in R

余生长醉 提交于 2019-11-29 13:39:54
I'm partitioning a data frame with split() in order to use parLapply() to call a function on each partition in parallel. The data frame has 1.3 million rows and 20 cols. I'm splitting/partitioning by two columns, both character type. Looks like there are ~47K unique IDs and ~12K unique codes, but not every pairing of ID and code are matched. The resulting number of partitions is ~250K. Here is the split() line: system.time(pop_part <- split(pop, list(pop$ID, pop$code))) The partitions will then be fed into parLapply() as follows: cl <- makeCluster(detectCores()) system.time(par_pop <-

Error in calling `lm` in a `lapply` with `weights` argument

喜欢而已 提交于 2019-11-29 13:38:34
I've encounter a weird behavior when calling lm within a lapply using the weights argument. My code consist of a list of formula on which I run a linear model that I call in lapply . So far it was working: dd <- data.frame(y = rnorm(100), x1 = rnorm(100), x2 = rnorm(100), x3 = rnorm(100), x4 = rnorm(100), wg = runif(100,1,100)) ls.form <- list( formula(y~x1+x2), formula(y~x3+x4), formula(y~x1|x2|x3), formula(y~x1+x2+x3+x4) ) res.no.wg <- lapply(ls.form, lm, data = dd) However, when I add the weights argument, I get a weird error: res.with.wg <- lapply(ls.form, lm, data = dd, weights = dd[,"wg"

Combining lists of different lengths into data frame

非 Y 不嫁゛ 提交于 2019-11-29 13:03:09
I have data like the SampleData below, which has lists of different length that I'd like to combine in to a data frame like the Desired Result below. I've tried using lapply and cbind.na from the qpcR package like the example below, but for some reason it won't let me turn the result into a data frame. If I just used two of the lists and cbind.na it will combine them and add the NA to the end like I want, but when I try using it in lapply it just leaves them as a list of different length lists. Any tips are greatly appreciated. SampleData<-list(list(1,2,3),list(1,2),list(3,4,6,7)) Desired

Using lapply on a list of models

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-29 11:47:21
I have generated a list of models, and would like to create a summary table. As and example, here are two models: x <- seq(1:10) y <- sin(x)^2 model1 <- lm(y ~ x) model2 <- lm(y ~ x + I(x^2) + I(x^3)) and two formulas, the first generating the equation from components of formula get.model.equation <- function(x) { x <- as.character((x$call)$formula) x <- paste(x[2],x[1],x[3]) } and the second generating the name of model as a string get.model.name <- function(x) { x <- deparse(substitute(x)) } With these, I create a summary table model.list <- list(model1, model2) AIC.data <- lapply(X = model

combination of expand.grid and mapply?

我与影子孤独终老i 提交于 2019-11-29 10:06:18
I am trying to come up with a variant of mapply (call it xapply for now) that combines the functionality (sort of) of expand.grid and mapply . That is, for a function FUN and a list of arguments L1 , L2 , L3 , ... of unknown length , it should produce a list of length n1*n2*n3 (where ni is the length of list i ) which is the result of applying FUN to all combinations of the elements of the list. If expand.grid worked to generate lists of lists rather than data frames, one might be able to use it, but I have in mind that the lists may be lists of things that won't necessarily fit into a data