lapply

getSymbols and using lapply, Cl, and merge to extract close prices

怎甘沉沦 提交于 2019-11-28 00:35:24
I've been messing around with this for some time. I recently started using the quantmod package to perform analytics on stock prices. I have a ticker vector that looks like the following: > tickers [1] "SPY" "DIA" "IWM" "SMH" "OIH" "XLY" "XLP" "XLE" "XLI" "XLB" "XLK" "XLU" "XLV" [14] "QQQ" > str(tickers) chr [1:14] "SPY" "DIA" "IWM" "SMH" "OIH" "XLY" "XLP" "XLE" ... I wrote a function called myX to use in a lapply call to save prices for every stock in the vector tickers. It has the following code: myX <- function(tickers, start, end) { require(quantmod) getSymbols(tickers, from=start, to=end)

Loop linear regression and saving coefficients

放肆的年华 提交于 2019-11-28 00:31:01
This is part of the dataset (named "ME1") I'm using (all variables are numeric): Year AgeR rateM 1 1751 -1.0 0.241104596 2 1751 -0.9 0.036093609 3 1751 -0.8 0.011623734 4 1751 -0.7 0.006670552 5 1751 -0.6 0.006610552 6 1751 -0.5 0.008510828 7 1751 -0.4 0.009344041 8 1751 -0.3 0.011729740 9 1751 -0.2 0.010988005 10 1751 -0.1 0.015896107 11 1751 0.0 0.018190140 12 1751 0.1 0.024588340 13 1751 0.2 0.029801362 14 1751 0.3 0.044515912 15 1751 0.4 0.055240354 16 1751 0.5 0.088476758 17 1751 0.6 0.119045309 18 1751 0.7 0.167866571 19 1751 0.8 0.239244825 20 1751 0.9 0.329683010 21 1751 1.0 0

Add “filename” column to table as multiple files are read and bound

痴心易碎 提交于 2019-11-28 00:15:57
I have numerous csv files in multiple directories that I want to read into a R tribble or data.table. I use "list.files()" with the recursive argument set to TRUE to create a list of file names and paths, then use "lapply()" to read in multiple csv files, and then "bind_rows()" stick them all together: filenames <- list.files(path, full.names = TRUE, pattern = fileptrn, recursive = TRUE) tbl <- lapply(filenames, read_csv) %>% bind_rows() This approach works fine. However, I need to extract a substring from the each file name and add it as a column to the final table. I can get the substring I

Why do rapply and lapply handle NULL differently?

﹥>﹥吖頭↗ 提交于 2019-11-27 23:16:23
问题 I'm aware that NULL values in lists can sometimes trip people up. I'm curious why in a specific instance lapply and rapply seem to treat NULL values differently. l <- list(a = 1, c = NULL, d = 3) lapply(l,is.null) $a [1] FALSE $c [1] TRUE $d [1] FALSE So far so good. How about if we do the exact same thing with rapply ? rapply(l, is.null, how = "replace") $a [1] FALSE $c list() $d [1] FALSE This example is very simple and non-recursive, but you see the same behavior in rapply with nested

R name colnames and rownames in list of data.frames with lapply

微笑、不失礼 提交于 2019-11-27 22:51:32
问题 I'm pretty frustrated because I dont know how I achieve the naming of the columns and rows in a list of data.frames. I mean I want to avoid using a loop. So I figured I could use just lapply. Ok at first I have the following list: >a $nem.greedyMAP.FALSE.POS X1 X2 X3 X4 X5 X6 X7 X8 X9 X10 1 NA NA NA NA NA NA NA NA NA NA 2 NA NA NA NA NA NA NA NA NA NA 3 NA NA NA NA NA NA NA NA NA NA 4 NA NA NA NA NA NA NA NA NA NA 5 NA NA NA NA NA NA NA NA NA NA 6 NA NA NA NA NA NA NA NA NA NA 7 NA NA NA NA

Using lapply with changing arguments

风格不统一 提交于 2019-11-27 20:18:55
R textbooks continue to promote the use of lapply instead of loops. This is easy even for functions with arguments like lapply(somelist, f, a=1, b=2) but what if the arguments change depending on the list element? Assume my somelist consists of: somelist$USA somelist$Europe somelist$Switzerland plus there is anotherlist with the same regions and I want use lapply with these changing arguments? This could be useful when f was a ratio calculation for example. lapply(somelist, f, a= somelist$USA, b=anotherlist$USA) Is there are way except for a loop to run through these regions efficiently? EDIT:

What are the advantages of the “apply” functions? When are they better to use than “for” loops, and when are they not? [duplicate]

你说的曾经没有我的故事 提交于 2019-11-27 18:13:26
问题 Possible Duplicate: Is R's apply family more than syntactic sugar Just what the title says. Stupid question, perhaps, but my understanding has been that when using an "apply" function, the iteration is performed in compiled code rather than in the R parser. This would seem to imply that lapply, for instance, is only faster than a "for" loop if there are a great many iterations and each operation is relatively simple. For instance, if a single call to a function wrapped up in lapply takes 10

Using get inside lapply, inside a function

点点圈 提交于 2019-11-27 17:48:35
问题 this may seem like a overly complicated question, but it has me driving me a little nuts for some time. It is also for curiosity, because I already have a way of doing what I need, so is not that important. In R, I need a function to return a named list object with all the arguments and the values entered by the user. For this I have made this code (toy example): foo <- function(a=1, b=5, h='coconut') { frm <- formals(foo) parms <- frm for (i in 1:length(frm)) parms[[i]] <- get(names(frm)[i])

Dataframes in a list; adding a new variable with name of dataframe

≯℡__Kan透↙ 提交于 2019-11-27 13:03:44
问题 I have a list of dataframes which I eventually want to merge while maintaining a record of their original dataframe name or list index. This will allow me to subset etc across all the rows. To accomplish this I would like to add a new variable 'id' to every dataframe, which contains the name/index of the dataframe it belongs to. Edit: "In my real code the dataframe variables are created from reading multiple files using the following code, so I don't have actual names only those in the 'files

How to index an element of a list object in R

…衆ロ難τιáo~ 提交于 2019-11-27 12:26:00
I'm doing the following in order to import some txt tables and keep them as list: # set working directory - the folder where all selection tables are stored hypo_selections<-list.files() # change object name according to each species hypo_list<-lapply(hypo_selections,read.table,sep="\t",header=T) # change object name according to each species I want to access one specific element, let's say hypo_list[1]. Since each element represents a table, how should I procced to access particular cells (rows and columns)? I would like to do something like it: a<-hypo_list[1] a[1,2] But I get the following