Questions tagged [lapply]

lapply is a function in R that returns a list of the same length as given argument X, each element of which is the result of applying given function to the corresponding element of X

3969 questions
1
vote
1 answer

Random generation of different variables in the same data frame

Lets say i have following two sets. set1=sample(c("White","Black"), 20,prob = c(0.6,0.4),replace = TRUE) set2=sample(c("yes","no"), 20,prob = c(0.48,0.32),replace = TRUE) from this i need to generate 20 (random)times where each…
student_R123
  • 962
  • 11
  • 30
1
vote
1 answer

display predicted values for initial data using auto.arima in R

Let's work with this data sample timeseries<-structure(list(Data = structure(c(10L, 14L, 18L, 22L, 26L, 29L, 32L, 35L, 38L, 1L, 4L, 7L, 11L, 15L, 19L, 23L, 27L, 30L, 33L, 36L, 39L, 2L, 5L, 8L, 12L, 16L, 20L, 24L, 28L, 31L, 34L, 37L, 40L, 3L, 6L,…
psysky
  • 3,037
  • 5
  • 28
  • 64
1
vote
1 answer

How to chain 2 lapply functions to subset dataframes in R?

I have a list containing 3 dataframes and another list containing 3 vectors of IDs. I'd like to subset each dataframe by checking if the IDs in the 1st dataframe match the ones in the first vector. Same for the second df and 2nd vector and 3rd df…
The Governor
  • 302
  • 1
  • 9
1
vote
3 answers

How to build a data frame from a nested list in R

I have a function that queries a database and returns a list of two data frames (df1 and df2). If I lapply iteratively over that function, I return a list of nested lists with the two data frames. The resultant list is structured as below: #e.g.…
Brisbane Pom
  • 521
  • 7
  • 18
1
vote
1 answer

Group dataframes stored in a list into separate, new dataframes

I have a list (dflist) that contains dataframes (dfX) that contain measurements for a collection of samples (e.g. samples 1-3; samp). Each dataframe itself contains measurements for a specific sample as measured using a specific instrument (e.g.…
Dunois
  • 1,813
  • 9
  • 22
1
vote
1 answer

Aggregate dataframes in a list by date and then merge back - lapply?

I'm struggling to figure out how to aggregate and then merge a list of dataframes. Has anyone done this? Here's the example. I have a list of dataframes created by splitting on a categorical variable. list1df 93 obs of 6 variables: -…
Christopher Penn
  • 539
  • 4
  • 14
1
vote
2 answers

R - How to subset all dataframes stored in a list according to a vector of conditions

This is my first time asking a question here so please let me know if I need to change the way I am doing this. I have been looking for awhile and I haven't been able to find what I need. I have a list of 3 dataframes. They have the same structure…
The Governor
  • 302
  • 1
  • 9
1
vote
1 answer

lapply error - PGLS (caper) with multiples comparative.data

i need help in the following problem. I generated a list containing 1000 comparative.dataand i want to run 1000 pgls using each of these comparative.data. I tried to use lapply function for this, using the following code: pg <- lapply(obj,…
tales_alencar
  • 141
  • 1
  • 10
1
vote
1 answer

Filtering list of list values in R without using for

I am translating looping for() operations into apply() family ones: (sapply, lapply, mapply, etc.) to gain performance in all my R code. I have a list of lists named lt with the following look: lt $`11141550000` y 2016-02-05…
NuValue
  • 453
  • 3
  • 11
  • 28
1
vote
1 answer

Join residual by group in summary Forecast table in R

reproducible example df=structure(list(group = c(1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L), year = c(1973L, 1974L, 1975L, 1976L, 1977L, 1978L, 1973L, 1974L, 1975L,…
psysky
  • 3,037
  • 5
  • 28
  • 64
1
vote
1 answer

calculating mean squared prediction error based on simulated data in R

I have following data. My goal is to calculate mean squared prediction error (MSPE)using cross validation by replicating 100 times. y=rnorm(250,0,2) x1=rnorm(250,0,3) x2=rnorm(250,1,1) datasim=data.frame(y,x1,x2) From this first i need…
student_R123
  • 962
  • 11
  • 30
1
vote
1 answer

Loop through statistical models and plot the fitted response curve

I have a series of statistical models in a list and I would like to plot the coefficients of these. I have various responses and explanatory variables within this list of models, as well as various statistical functions (e.g. exponential and…
James White
  • 705
  • 2
  • 7
  • 20
1
vote
3 answers

Combine Series of Columns in R

I have a dataframe where I need to combine every tenth column. As an example my dataframe looks like this: V1 V2 V3 V4 V5 V6 A B C D E F What I'd like is: first second third AD BE CF to do this I've been…
vino88
  • 163
  • 13
1
vote
1 answer

How to compute (i+1 - i) in a list of vectors

I have a list with 29 vectors (each vector with different lengths) like this: my_list [1] 1 12 23 34 38 [2] 2 12 21 38 47 56 71 . . [29] 14 22 81 88 91 94 I need to compute ( i+1 - i ) for each vector of the list (my_list). Example:…
user8769986
1
vote
2 answers

Eliminate for-loop through functional programming

I would like to take input from a dataframe with a systemName variable and a popNum variable and use this to generate a named list of vectors whose elements are the random numbers (1-6)*5 ie (5, 10, 15, 20, 25, 30) where the vector length is equal…
Justin In Oz
  • 148
  • 8
1 2 3
99
100