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
1 answer
Undefined columns error when using lapply
Why does this work and not the lapply?
Using the built in base R ChickWeight data:
names(ChickWeight)<-tolower(names(ChickWeight))
This works if I just want one correlations for 1 column,…

CrunchyTopping
- 803
- 7
- 17
1
vote
1 answer
Finding range of numbers on end of URL in R
I'm trying to get the range of the numbers at the end of this link: https://schedule.sxsw.com/2019/speakers/2008434.
The link has a number at the end, e.g. the 2008434. The links refer to the bios of speakers at the upcoming South by Southwest…

papelr
- 468
- 1
- 11
- 42
1
vote
1 answer
Replace special character with lapply and gsub in rstudio
I am trying to clean up my data by applying the following codes:
Manuf <- lapply(Manuf, gsub, pattern ='%', replacement ='')
Manuf <- lapply(Manuf, gsub, pattern='\\$', replacement ='')
I noticed the moment I applied the code, it turned my data…

Amy Chen
- 25
- 4
1
vote
1 answer
Looping all pairwise comparisons from a list in R
In my R code below, suppose I want to compare all unique 2 m objects using a similar R routine. For example, to compare m1 and m2, my routine is:
pchisq(2 * (logLik(m2) - logLik(m1)), df = abs(m1$df.residual - m2$df.residual), lower = F)
Question:
I…

rnorouzian
- 7,397
- 5
- 27
- 72
1
vote
3 answers
Access the row number in lapply
I would like to access the current row number in a lapply-iteration:
lapply(dplyr::starwars$name[1:3], function(x){
lapply(dplyr::starwars$name[2:4], function(y){
paste(x,'&',y)
})
})
In the second lapply-statement I need to access the…

Marvin
- 21
- 1
1
vote
1 answer
simulate lapply / loop over a list of parameters
I would like to simulate the frequency and severity over a list of parameters.
Here is the case for the first item in the list:
data <- data.frame(
lamda = c(5, 2, 3),
meanlog = c(9, 10, 11),
sdlog = c(2, 2.1, 2.2))
freq <- rpois(s,…

Vincent Risington
- 167
- 8
1
vote
3 answers
Adding differing chars to list of dataframes via lapply
I want to add individual elements of a char vector as columns to a list of data.frames. I can do it manually, but is there a more elegant lapply-way?
# Create sample dfs
set.seed(1)
df1 <- data.frame("one" = sample(1:10,10,replace=TRUE),
…

Krisselack
- 503
- 3
- 16
1
vote
1 answer
Why is R telling me "invalid subscript type 'list' " when I try to extract variables from an nls() result?
transgression2 is a list with all of the nls results from 50000 nls runs stored in it. I'm trying to extract the coefficients from it.
Doing transgression2[[1]]$m$getAllPars() returns a named number list.
nlsCoefficients <- lapply(transgression2,…

Fake Fake
- 11
- 1
1
vote
0 answers
Why is function returning Data values encoded with ÿþ if used with lapply?
here is my function defintion
get_df <- function(c_code) {
df %>%
filter(
country_code == c_code
)
}
following is my sample dataframe
x<- c("abc", "USA", "ghi", "jkl")
y<- c("bnj", "GBR", "sfd", "lai")
df <- data.frame(rbind(x,…

Ravi Shankar Hela
- 93
- 1
- 11
1
vote
0 answers
Parallel: Imitate monitoring when using parLapply like pblapply does
I would like to know if there is a way to monitorize a process using parLapply (parallel) like pblapply does. Here is just a simple code to try it:
library(parallel)
library(pbapply)
f <- function (x) {return <- (x^2)^2}
f.list <-…

César Arquero Cabral
- 481
- 5
- 22
1
vote
2 answers
rank values using list and data frame
I have a list and a data frame
l <- list("a" = c(1, 2), "b" =c(1, 3))
id value
a 3
b 2
I want to get the rank of value in the data frame by id considering both list and data frame by matching id with list name. For example, if we…

YellowRiver
- 65
- 1
- 7
1
vote
2 answers
apply function by name of list
Imagine that I have a list
l <- list("a" = 1, "b" = 2)
and a data frame
id value
a 3
b 4
I want to match id with list names, and apply a function on that list with the value in data frame. For example, I want the sum of value in…

YellowRiver
- 65
- 1
- 7
1
vote
1 answer
Column name in variable using data.table and lapply
I have data similar to this:
set.seed(1)
testing1 <- data.table(type=c("stock","stock","bond","bond"),a=rnorm(4),b=rnorm(4),c=rnorm(4),d=rnorm(4),e=rnorm(4))
type a b c d e
1: stock -0.6264538 …

FG7
- 469
- 4
- 14
1
vote
0 answers
Apply dplyr::count_ to many data frames
I got error for apply count_ to all my dataframes. I can manually apply to single dataframe, but when I tried lapply, it showed error
Error in UseMethod("groups") :
no applicable method for 'groups' applied to an object of class "character"
I…

Hui
- 25
- 5
1
vote
3 answers
lapply() to use a function over multiple columns of a dataframe
I am tracking the body weights of individuals over time, and the function below allow me to calculate the % body weight of the individual on a particular day, relative to the initial value (essentially dividing the body weight on a particular day by…

ThomasC
- 13
- 1
- 4