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
Questions tagged [lapply]
3969 questions
1
vote
4 answers
deleting an element from all embedded lists in a list in R
I have a list (g) of lists as shown below. To delete element k from one of the embedded lists (ex. 1st list), I can do: g[[1]]$k <- NULL.
But when I try to delete element k from all embedded lists, my lapply code fails? Is there a Base R fix?
g <-…

rnorouzian
- 7,397
- 5
- 27
- 72
1
vote
1 answer
Is there lapply-like function for S4 object?
To apply a function to all slots in S4.
Of course, it can be done with for-loop over slotNames(). But I'm curious if it can be done in a vectorized way.

yuk
- 19,098
- 13
- 68
- 99
1
vote
2 answers
How to run multiple linear regressions with different independent variables and dependent variables adding standardized coefficients in R?
I'm currently trying to run a loop performing linear regression for multiple independent variables (n = 6) with multiple dependent variables (n=1000).
Here is some example data, with age, sex, and education representing my independent variables of…

M_Oxford
- 361
- 4
- 11
1
vote
2 answers
Create multiple dataframes by filtering subsets that are not equal to a value in R
I have a dataframe with two columns Text and Colours.
library(tidyverse)
library(purrr)
# sample dataframe
df <- data.frame(Text = c("text1", "text2", "text3", "text4"),
Colours = c("blue", "white", "green", "yellow"),…

yiah
- 81
- 7
1
vote
1 answer
Matching pairs in a list of named data.frames in R
Suppose I have 2 lists of named (ex. Study1) data.frames.
I want each element d from each data.frame in the 2nd list (k) to be paired with the single corresponding d element in the 1st list (j) for the same data.frame.
For example, in the below…

rnorouzian
- 7,397
- 5
- 27
- 72
1
vote
1 answer
How to extract second value from a list of lists and give NA to lists with no second value?
I'd like to extract the first and second values from a list of lists. I was able to extract the first value with no issue. However, it gives me an error when I was trying to extract the second value because not all lists from the suggestion column…

cheklapkok
- 439
- 1
- 5
- 11
1
vote
0 answers
Avoid loop with lapply character variable
I would like to use lapply instead of a for loop. It's about to summarize multiple variables to create new ones like ..@3, ...@6, etc.
i tried to use noquote to get rid of the "", but i always get the following error message:
Error in…

Bier Basti
- 11
- 3
1
vote
1 answer
R: how to use lapply for monthly data
I have a data set which has the below data:
Ticket Feed back Date Month Rating
12345 The resolution was proper 01-01-2019 January 5
12346 The ticket was closed…

AlisonGrey
- 497
- 1
- 7
- 23
1
vote
2 answers
How to order models from best to worst based on AIC from the result of lapply in R
Suppose I have following df.
ind1 <- rnorm(100)
ind2 <- rnorm(100)
ind3 <- rnorm(100)
ind4 <- rnorm(100)
ind5 <- rnorm(100)
dep <- rnorm(100, mean=ind1)
df <- data.frame(dep,ind1, ind2, ind3, ind4, ind5)
I calculated 3rd order polynomial…

R starter
- 197
- 12
1
vote
4 answers
Create new column that equals one of many existing columns when condition is met
I have a wide dataframe with election results. I need to make a new column saying how many votes a specific party received. The way the votes are recorded means that I need to loop over a large number of columns to do this. I can see how to do it…

lethalSinger
- 606
- 3
- 9
1
vote
1 answer
Applying 'clustering functions' to a series of linear models
I want to iterate over a list of linear models and apply "clustered" standard errors to each model using the vcovCL function. My goal is to do this as efficiently as possible (I am running a linear model across many columns of a dataframe). My…

Thomas Bilach
- 591
- 2
- 16
1
vote
2 answers
How to use multiple functions based on a condition using lapply
I have a question regarding using multiple functions in lapply using a condition.
Here is the data frame
Data <- data.frame(c(1:6),c(7:12), c(1,0,1,0,1,1), 0)
colnames(Data) <- c("a","b","c","d")
I want an out put applied to each row based on a…

Kou
- 175
- 1
- 9
1
vote
2 answers
Use lapply on subset of columns with written function?
I have a dataset in which certain columns are dates in character form.
The dates are inconsistent in their formatting and missing data are present. I wrote a code to transform them in the correct format.
If I use the code for each column with lapply…

MCS
- 1,071
- 9
- 23
1
vote
1 answer
Number the dataframes within the list (i.e., create a column with an individual number for each dataframe)
I need to be able to distinguish the dataframes within the large list with an individual number, so that I can group_bybased on that number after binding the dataframes (in my case it is impossible to just work with a list without…

aazh
- 91
- 1
- 8
1
vote
2 answers
Subset list of vectors by position in a vectorized way
I have a list of vectors and I'm trying to select (for example) the 2nd and 4th element in each vector. I can do this using lapply:
list_of_vec <- list(c(1:10), c(10:1), c(1:10), c(10:1), c(1:10))
lapply(1:length(list_of_vec), function(i)…

Rob Marty
- 378
- 1
- 9