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
Using lapply in server to generate render UI
I have some data like this subexample
structure(list(monthyr = structure(c(17287, 17287, 17287, 17287,
17287, 17287, 17287, 17287, 17287, 17287, 17287, 17287, 17287,
17287, 17287, 17287, 17287, 17287), class = "Date"), Location = c("TAI",
"NAM",…

user195366
- 465
- 2
- 13
1
vote
1 answer
Apply an R function to one dataset multiple times, but change the value of one argument each time
I want to write/apply a function that repeats the LD2 function from the pegas package on a dataset (jaguar), but changes the value of the 'locus' argument each time. The 'locus' argument accepts a vector of length two (e.g. c(1,2), c(2,3), c(77,78),…

Tom__Jenkins
- 129
- 6
1
vote
1 answer
How to apply function to rows and then bind the results?
I have a data frame containing latitude and longitude coordinates. The function I have returns a one row data frame with data about those coordinates. I want to apply this function for all of the coordinates in the data frame and then bind these…

Casey Charlesworth
- 25
- 4
1
vote
2 answers
missing argument when using lapply in R
I have the following dataframe:
set.seed(1)
df <- data.frame(X1 = sample(c(letters[1:5],NA),10,replace=TRUE),
X2 = sample(c(letters[1:5],NA),10,replace=TRUE),
X3 = sample(c(letters[1:5],NA),10,replace=TRUE),
…

Chris
- 2,019
- 5
- 22
- 67
1
vote
1 answer
apply a function which contains group_by within dplyr funciton to datalist in R
I have a data.list like so:
list(structure(list(group = c(1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L
), species = structure(c(3L, 3L, 1L, 3L, 3L, 2L, 3L, 1L, 3L,
1L, 3L, 1L, 3L, 1L, 2L, 4L, 1L,…

R starter
- 197
- 12
1
vote
1 answer
Apply function over every entry one table to every entry of another
I would like to apply a function, bandedlossfn to all entries in loss.tib using every item in bandstib
library(tidyverse)
set.seed(1)
n <- 5
loss <- rbeta(n, 1, 10) * 100
loss.tib <- loss %>% as_tibble %>% mutate(loss = value) %>% mutate(lossid =…

Vincent Risington
- 167
- 8
1
vote
1 answer
how to use lappy for string replacement
I am trying to use lapply to replace the elements of a string in several data.frames contained in a list. When I attempt to do this, the whole data.frame is replaced, rather than the string contained in the data.frame.
A reproducible example below:…

Adam_123
- 159
- 9
1
vote
1 answer
How to Use 'lapply' on Sublists (r)
I'm trying to use lapply on a list of sublists. However all I can manage is overwriting the sublists with what should be going into them, rather than writing into the sublists.
To make my problem clear to the point of tedium, this would be the same…

Solebay Sharp
- 519
- 7
- 24
1
vote
3 answers
Filtering dataframes in named list based on the name of each dataframe
I have multiple data frames in a named list, reproducible example below:
set.seed(1)
df1 <- data.frame(
numbers = sample(1:10),
boolean = sample(c("yes", "no"), 10, replace = TRUE)
)
df2 <- data.frame(
letters = sample(LETTERS,10),
…

Beeba
- 642
- 1
- 7
- 18
1
vote
1 answer
Inconsistent dates formatting (lubridate fail)
I have a vector of inconsistent dates, including (mainly) these three formats:
"%d/%m/%y", "%m/%d/%y" and "%d/%m/%Y"
I tried to implement this:
df <- as.data.frame(c("30/12/00","7/31/09","17/09/2008"),col.names = "original_date")
guess_date <-…

MCS
- 1,071
- 9
- 23
1
vote
1 answer
Using lapply function to separate columns that consist of two columns
I have a data like here. The data contains rainfall values for 3 years (2005-2006-2007) separately.
Data:
mydata<-structure(list(X2005.hourly = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…

Hüsamettin Tayşi
- 564
- 3
- 18
1
vote
2 answers
R For loop with if statement to Apply format
Say I have a for loop that looks like this.
person <- c("Mike", "Kim", "Fred", "Steve", "Quail", "bean")
sleep <- c(5, 10, 3, 7, 1, 5)
dat <- data.frame(person, sleep)
for (i in 1:length(dat$person)) {
if (dat$sleep[i] >= 7) {
dat$fatigue[i]…

mrsquid
- 605
- 2
- 9
- 24
1
vote
1 answer
How to create complex hierarchies of lists in R
I have received help on how to create a set of lists within lists, however I have been unable to add another layer / extend the depth of my lists. All I want is to add a final 'layer' in each list, that reads, 'DataFrame', 'DataFrame2', and so on.…

Solebay Sharp
- 519
- 7
- 24
1
vote
1 answer
How to add a character when saving files with lapply
I have the following list
L = list.files(".", ".txt")
which is
a.txt
b.txt
c.txt
and I want to apply some code to all files in that list, but I want to save the dataframes with the samename plus some character to indicate that it is…

Gabriel G.
- 555
- 1
- 3
- 13
1
vote
2 answers
R: plotting title and abline for multiple plots generated with lapply
I am generating multiple plots using lapply to retrieve data from a vector for each of the plots.
Problem:
I need to plot the abline for each graph, but it plots only in the last one.
Ideally, I would also plot a different title for each of the…

InspiredCat
- 27
- 2
- 7