lapply

Using lapply to subset rows from data frames — incorrect number of dimensions error

馋奶兔 提交于 2019-12-13 16:12:08
问题 I have a list called "scenbase" that contains 40 data frames, which are each 326 rows by 68 columns. I would like to use lapply() to subset the data frames so they only retain rows 33-152. I've written a simple function called trim() (below), and am attempting to apply it to the list of data frames but am getting an error message. The function and my attempt at using it with lapply is below: trim <- function(i) { (i <- i[33:152,]) } lapply(scenbase, trim) Error in i[33:152, ] : incorrect

how paste function working in R? [duplicate]

不打扰是莪最后的温柔 提交于 2019-12-13 10:37:53
问题 This question already has an answer here : multiply multiple column and find sum of each column for multiple values (1 answer) Closed 6 years ago . this is my code here the paste function works for only two combination.i need the same code in a loop for more than two combinations at the same time. i<-2 while (i<=10) { results<-data.frame() results<- t(apply(data,1,function(x) combn(x,i,prod))) comb <- combn(colnames(data),i) colnames(results) <- apply(comb,i,function(x) paste(x[1],x[2])) i<-i

How to extract coefficients outputs from a linear regression with loop

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-13 08:48:32
问题 I would like to know how I can loop a regression n times, and in each time with a different set of variables, extract a data.frame where each column is a regression and each row represent a variable. In my case I have a data.frame of: dt_deals <- data.frame(Premium=c(1,3,4,5),Liquidity=c(0.2,0.3,1.5,0.8),Leverage=c(1,3,0.5,0.7)) But I have another explanatory dummy variable called hubris , that is the product of a binomial distribution, with 0.25 of mean. Like that: n <- 10 hubris_dataset <-

felm function doesn't work inside lapply

℡╲_俬逩灬. 提交于 2019-12-13 07:36:39
问题 This is a follow-up question from Automate regression with specific dependent and independent variables This code works a treat with plm function from plm package. However, I wanted to estimate the same model with felm function from lfe package to obtain standard errors clustered by space (in my case county). Let this be the data set: regDf <- data.frame(year = rep(seq(1966,2015,1), 8), county = c(rep('first', 50), rep('second', 50), rep('third', 50), rep('fourth', 50), rep('fifth', 50), rep(

How to save(assign) the output from lapply into individual variables in R?

泄露秘密 提交于 2019-12-13 07:27:22
问题 I have a very simple question but I've stocked in it! I have a list as an output : as an example : lapply(1:40, function(x){ (1+x)}) output: [[1]] [1] 2 [[2]] [1] 3 [[3]] [1] 4 till [[40]] [1] 41 I wondered how can I assign each of the outputs of this list to a new variable by using a loop? something like this a<- [[2]]; b <- [[3]]; ... az <- [[40]] and so one. Thanks in advance, 回答1: Here is my solution, it requires that your output of lapply is stored with the name ll but it can be easily

Loop through several post hoc tests in R

好久不见. 提交于 2019-12-13 07:24:28
问题 I have a dataframe called data. I have created a function that loop thorugh a list of variables and creates a linear model for each of them using lapply. This method is based on this post. library(datasets) testDF <- data.frame(Salaries) #creates list of variables varListTest <- names(testDF)[3:4] #creates a model for each of the variables in question model<- lapply(varListTest, function(x) { lm(substitute(i~Rank, list(i = as.name(x))), data = testDF)}) #output model lapply(model, summary)

How to use lapply to put elements of a list into different data frames

故事扮演 提交于 2019-12-13 06:13:48
问题 I have a list of dataframes called db; each data frame has its name. I use: lapply(names(db), function(x)write.csv(db[x], file =paste0(x,'.csv'))) to extract the d.frames and save them into csv files. Now i'm trying to extact from the list the dataframes and create different dataframes with this command: lapply(names(db), function(x)as.data.frame(db[x])) But it does not store any data frame into the workspace , how can I store each df with different names into the workspace? 回答1: We extract

print list names while iterating in lapply not working in 3.2

笑着哭i 提交于 2019-12-13 05:26:06
问题 I'm trying o output the list names every time I run the function thru lapply . I posted this question earlier that I posted earlier, and the answer provided by @Ananda Mahto worked fine until I upgraded my R to version 3.2.0. It is no longer working and I get the following error message: Error in eval.parent(quote(names(X)))[substitute(x)[[3]]] : invalid subscript type 'symbol' x <- ts(rnorm(40,5), start = c(1961, 1), frequency = 12) y <- ts(rnorm(50,20), start = c(1971, 1), frequency = 12) z

lapply instead of for loop

倾然丶 夕夏残阳落幕 提交于 2019-12-13 05:14:47
问题 I have the following huge dataframe : V1 V2 V3 V4 A E R 12 A R T 18 A T Y 44 A Y U 11 B E R 22 B R T 53 B T Y 11 B Y U 153 what im trying to do is to get the outlier value from V4 for each pair of (V1,V2) This easily handled with 2 for loops based on the unique values of V1 and V2 and a subset for each round, take the vector of V4 for each subset and get the outlier using any function of the outlier package, but the the problem is then speed. i have never used lapply , maybe someone can guide

Forecat : incorrect number of dimensions in R [closed]

青春壹個敷衍的年華 提交于 2019-12-13 04:32:53
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed last year . I conduct TS analysis. My dput(). df=structure(list(month = structure(c(5L, 4L, 8L, 1L, 9L, 7L, 6L, 2L, 12L, 11L, 10L, 3L, 5L, 4L, 8L, 1L, 9L, 7L, 6L, 2L, 12L, 11L, 10L, 3L, 5L, 4L, 8L, 1L, 9L, 7L, 6L, 2L, 12L, 11L, 10L, 3L, 5L, 4L, 8L, 1L, 9L, 7L, 6L, 2L, 12L, 11L, 10L, 3L, 5L, 4L, 8L, 1L, 9L, 7L, 6L, 2L, 12L,