lapply

Use R and Openxlsx to output a list of dataframes as worksheets in a single Excel file

不想你离开。 提交于 2019-12-12 08:42:41
问题 I have a set of CSV files. I want to package them up and export the data to a single Excel file that contains multiple worksheets. I read in the CSV files as a set of data frames. My problem is how to construct the command in openxlsx , I can do it manually, but I am having a list construction issue. Specifically how to add a data frame as a subcomponent of a named list and then pass as a parameter to write.xlsx() Example Ok, so I first list the CSV files on disk and generate a set of data

string split operation in R

孤街醉人 提交于 2019-12-12 04:52:50
问题 In my data I have a column of strings. Each string is five characters long. I would like to figure out how to split the string so that I keep the first two characters, the last two and disregard the middle or third character. I looked at other stackoverflow questions and found the answer listed below as helpful. Initially, the solution below was useful until I saw that in certain cases it didn't work or it worked in the way I wasn't expecting. This is what I have: statecensusFIPS <- c("01001"

Create a list of a list of dataframes, by subsetting a list of dataframes in R

半城伤御伤魂 提交于 2019-12-12 04:22:27
问题 I have a list of 6 dataframes, and I would like to create a list of 6 lists of 24 dataframes, the 24 dataframes being subsets of the original 6 dataframes. Here is a shorter example of what I'm trying to do: months <- c(0:35) product<- c(112:147) index <- rnorm(36) df1 <- data.frame(months, product, index) product2<- c(212:247) index2 <- rnorm(36) df2 <- data.frame(months, product2, index2) product3<- c(312:347) index3 <- rnorm(36) df3 <- data.frame(months, product3, index3) dflist <- list

Rugarch - Estimate models for various error distributions and input data

痞子三分冷 提交于 2019-12-12 03:56:58
问题 I am an R programming beginner. I need to repeat the code of the following post answer Adding EGARCH flavor into a loop over fGARCH-flavor models over two different arguments of the estimation function and in the following order. The first argument to reiterate over is the error distribution. Here is the vector which is supposed to change in the model estimation command (see linked post): error_distribution=c("norm","std","ged") model_2012=ugarchspec(variance.model = list(model="fGARCH",

R Apply function with if statements to every elements in list

假装没事ソ 提交于 2019-12-12 03:52:09
问题 I have a huge list, below is a sample of trboot6 UPDATE: I do not want to delete the extra "1" or "-1". Instead I want to change it to zero. I am so sorry dput() structure(list(`1` = c(-1, 1, -1, -1, -1, -1, -1, -1, -1, 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1), `2` = c(-1, -1, -1, 1, 1, 1, -1, -1, -1, -1, -1, -1, 1, 1, -1, -1, -1, -1, -1, 1, 1, 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 1, 1, -1, -1, -1, -1, -1,

How to split epochs into year, month, etc

一个人想着一个人 提交于 2019-12-12 03:09:46
问题 I have a data frame containing many time columns. I want to add columns for each time for year, month, date, etc. Here is what I have so far: library(dplyr) library(lubridate) times <- c(133456789, 143456789, 144456789 ) train2 <- data.frame(sent_time = times, open_time = times) time_col_names <- c("sent_time", "open_time") dt_part_names <- c("year", "month", "hour", "wday", "day") train3 <- as.data.frame(train2) dummy <- lapply(time_col_names, function(col_name) { pct_times <- as.POSIXct

R: How to sum multiple columns of matrices in a list? [duplicate]

≡放荡痞女 提交于 2019-12-12 02:40:44
问题 This question already has answers here : R: How to sum multiple columns of data frames in a list? (2 answers) Closed 3 years ago . I want to sum multiple columns of matrices in a list and only show the sum without showing the (calculation) input columns (similar to my former question on data frames). Thanks for the former answers, however I struggled to implement the ideas on matrices. Here an example: ls <- list(matrix(c(1, 5, 3, 2), ncol=4), matrix(c(NA, 2, 7, 9), ncol=4)) countries <- c("a

select first nth percent of rows from random sampled dataframes of list in r

柔情痞子 提交于 2019-12-12 02:30:59
问题 I wrote a function that selects first nth percent of rows (i.e., threshold) from dataframe and this works on dataframes of list as well. The functions is given below: set.threshold.rand <-function(value, vector){ print(length(vector)) n<-as.integer(length(vector)/100*value) threshold<-vector[n] return(threshold) } sensitivity.rand<-function(vector, threshold){ thresh<-set.threshold.rand(threshold, vector) print(thresh) score<-ifelse(vector<=thresh, "H", "L") # after taking the threshold

R code slowing with increased iterations

余生长醉 提交于 2019-12-12 01:43:39
问题 I've been trying to increase the speed of some code. I've removed all loops, am using vectors and have streamed lined just about everything. I've timed each iteration of my code and it appears to be slowing as iterations increase. ### The beginning iterations user system elapsed 0.03 0.00 0.03 user system elapsed 0.03 0.00 0.04 user system elapsed 0.03 0.00 0.03 user system elapsed 0.04 0.00 0.05 ### The ending iterations user system elapsed 3.06 0.08 3.14 user system elapsed 3.10 0.05 3.15

lapply to turn specified matrix elements within list to NA

与世无争的帅哥 提交于 2019-12-11 23:28:11
问题 I have a list of matrices (toy example): x <- matrix(1:20, nrow = 2, ncol = 10) y <- matrix(1:20, nrow = 2, ncol = 10) l <- list(x ,y) I need to turn some elements >= 11 into NA. Outside of the list I would just use x[(x>= 11)] <- NA but trying to lapply the same function appears to apply it to each matrix as a whole (ie each matrix turns into a single NA value). l_na <- lapply(l, function(x) x[(x >= 11)] <- NA) I'm clearly misunderstanding something about lapply. A solution and any pointers