lapply

Creating multiple dummies from an existing data frame or data table

无人久伴 提交于 2019-12-06 07:14:13
问题 I am looking for a quick extension to the following solution posted here. In it Frank shows that for an example data table test <- data.table("index"=rep(letters[1:10],100),"var1"=rnorm(1000,0,1)) You can quickly make dummies by using the following code inds <- unique(test$index) ; test[,(inds):=lapply(inds,function(x)index==x)] Now I want to extend this solution for a data.table that has multiple rows of indices, e.g. new <- data.table("id" = rep(c("Jan","James","Dirk","Harry","Cindy",

Reading function input values defined in `…` from an CSV file in R

狂风中的少年 提交于 2019-12-06 06:27:23
问题 Suppose I have an R function like foo below. This function has 4 fixed arguments, and any number of arbitrary arguments defined in ... . All input values for foo arguments are stored in THIS CSV file. In my code below, I can successfully run foo using the 4 fixed arguments imported from the CSV file in a lapply loop. BUT I'm wondering how I can insert the arguments defined in ... in the lapply command? foo <- function(n = NULL, r = NULL, post, control, ...){ ## the function data.frame(n = n,

obtaining average coefficients and adj. R^2 from multiple pooled regressions using lapply

孤者浪人 提交于 2019-12-06 06:16:12
I have performed multiple Pooled regressions with a loop function and stored the regression output in a list (myregression). What i would like to do now is to efficiently perform the coeftest function in the lmtest package over all my regressions (i.e. myregression list) to adjust standard errors and t-statistics. Finally i would like to obtain the mean of the coefficients, standard errors and t-values. Here is what i came up so far: library(plm) data("Grunfeld", package="plm") # Store each subset regression in myregression myregression <- list() count <- 1 # Regression on six-year subsets of

How to restore attribute after union n igraphs?

你说的曾经没有我的故事 提交于 2019-12-06 04:52:28
let's say I have n igraphs objects g1 , g2 ,.., gn . They are undirected and weighted graphs, i.e. new weight's attribute should be added. I'd like to union n graphs into the weighted graph g . It is known from the documentation (see ?graph.union ) if the n graphs have the weight attribute, it is renamed by adding a _1 and _2 (and _3 , etc.) suffix, i.e. weight_1 , weight_2 ,..., weight_n . I have seen the answer and wrote the code for n=3 graphs (see below). Edited: library(igraph) rm(list=ls(all=TRUE)) # delete all objects g1 <- graph_from_literal(A1-B1-C1) g2 <- graph_from_literal(A2-B2-C2)

How to download multiple closing stock prices only with getSymbols into separate xts files?

╄→гoц情女王★ 提交于 2019-12-06 04:23:30
How can I use getSymbols from the quantmod package to do the following: Download multiple stock price histories Select only the adjusted closing prices--i.e., suppress open/high/low and vol data Save each price history as a separate xts file, with dates I can implement steps 1 and 2, but I'm having trouble with step 3. StackOverflow has several posts on downloading and merging prices for multiple tickers, but I can't find instructions for downloading and saving in separate files. Here's what I have so far. Any advice on implementing the last step would be greatly appreciated. Thanks in advance

Performing loops on list of lists of rasters

扶醉桌前 提交于 2019-12-06 02:06:29
问题 Need solution, help will be much appreciated. In the following code I am creating three rasters. I then create a random number of point locations on this raster and I am receiving a list of three matrices with coordinates of those random locations called samples . I then take those locations and sample raster values to receive samplevalues . What I want to change is that I want to create a set of 100,150,200 and 250 random point locations ( numberv ). So after generating these locations and

How can I use attr<- with lapply?

谁都会走 提交于 2019-12-05 22:45:50
Or put it more general: How can I add multiple attributes to the elements of list? I am stuck trying to set an attribute to elements of a list all of which are data.frames . In the end I would like to add names(myList) as a varying attribute to every data.frame inside. But I even cannot get a static attribute for all list elements to go. lapply(myList,attr,which="myname") <- "myStaticName" This does not work because lapply does not work with lapply<- . If I had at least an idea how to do this, maybe I could figure out how to do it with varying attributes like the name of the list. I don't

Efficient sampling from nested lists

我只是一个虾纸丫 提交于 2019-12-05 19:29:49
问题 I have a list of lists , containing data.frames, from which I want to select only a few rows . I can achieve it in a for-loop, where I create a sequence based on the amount of rows and select only row indices according to that sequence. But if I have deeper nested lists it doesn't work anymore. I am also sure, that there is a better way of doing that without a loop. What would be an efficient and generic approach to sample from nested lists, that vary in their dimensions and contain data

R: How to run function on two lists?

主宰稳场 提交于 2019-12-05 18:52:03
I want to run the following function on two lists: function(Z, p) { imp <- as.vector(cbind(imp=rowSums(Z))) exp <- as.vector(t(cbind(exp=colSums(Z)))) x = p + imp ac = p + imp - exp einsdurchx = 1/as.vector(x) einsdurchx[is.infinite(einsdurchx)] <- 0 A = Z %*% diag(einsdurchx) R = solve(diag(length(p))-A) %*% diag(p) C = ac * einsdurchx R_bar = diag(as.vector(C)) %*% R rR_bar = round(R_bar) return(rR_bar) } which works fine on a matrix and a vector . However, I need to run this function on a list of matrices and a list of vectors . I tried so far lapply/mapply following this example , see

different behaviors when passing empty ellipsis arguments

安稳与你 提交于 2019-12-05 17:38:14
This answer brought up the question of how the ellipsis feature in R handles empty arguments. Apparently an empty argument in ... works sometimes (see lapply version below) but not other times (see sapply version). Here's the example: lst <- list(x=matrix(1)) lapply(lst, "[", 1, ) # $x # [1] 1 sapply(lst, "[", 1, ) # Error in lapply(X = X, FUN = FUN, ...) : # argument is missing, with no default From what I can tell, sapply actually just reuses its ... arguments when calling lapply . So I don't understand why lapply works but sapply doesn't. Can anybody explain this behavior. In the sapply