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
24
votes
1 answer
lapply function /loops on list of lists R
I know this topic appeared on SO a few times, but the examples were often more complicated and I would like to have an answer (or set of possible solutions) to this simple situation. I am still wrapping my head around R and programming in general.…

MIH
- 1,083
- 3
- 14
- 26
22
votes
4 answers
Same function over multiple data frames in R
I am new to R, and this is a very simple question. I've found a lot of similar things to what I want but not exactly it. Basically I have multiple data frames and I simply want to run the same function across all of them. A for-loop could work but…

user3272284
- 279
- 2
- 3
- 10
21
votes
1 answer
deparse(substitute(x)) in lapply?
I would like use a function that uses the standard deparse(substitute(x)) trick within lapply. Unfortunately I just get the argument of the loop back. Here's my completely useless reproducible example:
# some test data
a <- 5
b <- 6
li <-…

Matt Bannert
- 27,631
- 38
- 141
- 207
19
votes
6 answers
Union of intersecting vectors in a list in R
I have a list of vectors as follows.
data <- list(v1=c("a", "b", "c"), v2=c("g", "h", "k"),
v3=c("c", "d"), v4=c("n", "a"), v5=c("h", "i"))
I am trying to achieve the following:
Check whether any of the vectors intersect with each…

Crops
- 5,024
- 5
- 38
- 65
19
votes
6 answers
how to determine if a character vector is a valid numeric or integer vector
I am trying to turn a nested list structure into a dataframe. The list looks similar to the following (it is serialized data from parsed JSON read in using the httr package).
myList <- list(object1 = list(w=1, x=list(y=0.1, z="cat")), object2 =…

Andrew Barr
- 3,589
- 4
- 18
- 28
18
votes
3 answers
zipping lists in R
As a guideline I prefer apply functions on elements of a list using lapply or *ply (from plyr) rather than explicitly iterating through them. However, this works well when I have to process one list at a time. When the function takes multiple…

gappy
- 10,095
- 14
- 54
- 73
17
votes
7 answers
How can I apply a custom function that adds new columns to a dataframe to a subset of existing columns?
I am working with a large dataset where much of the data was entered twice. This means that many of the variables are represented by pairs of columns: column.1 with the data entered by one person, and column.2 where the same data was entered by a…

sf26749
- 171
- 2
17
votes
1 answer
Convert several columns from integer to numeric in R data.frame
I would like to convert columns from 2 to 13 (the last one) from integer to numeric.
For one column, I use the following code:
dades$V3 <- as.numeric(dades$V3)
I want to convert columns from 2 to 13 with the same command. I create this…

Enric Agud Pique
- 1,087
- 7
- 13
- 30
17
votes
3 answers
can lapply not modify variables in a higher scope
I often want to do essentially the following:
mat <- matrix(0,nrow=10,ncol=1)
lapply(1:10, function(i) { mat[i,] <- rnorm(1,mean=i)})
But, I would expect that mat would have 10 random numbers in it, but rather it has 0. (I am not worried about the…

stevejb
- 2,414
- 5
- 26
- 41
16
votes
1 answer
Why do rapply and lapply handle NULL differently?
I'm aware that NULL values in lists can sometimes trip people up. I'm curious why in a specific instance lapply and rapply seem to treat NULL values differently.
l <- list(a = 1, c = NULL, d = 3)
lapply(l,is.null)
$a
[1] FALSE
$c
[1] TRUE
$d
[1]…

joran
- 169,992
- 32
- 429
- 468
16
votes
5 answers
Is it possible to skip NA values in "+" operator?
I want to calculate an equation in R. I don't want to use the function sum because it's returning 1 value. I want the full vector of values.
x = 1:10
y = c(21:29,NA)
x+y
[1] 22 24 26 28 30 32 34 36 38 NA
x = 1:10
y = c(21:30)
x+y
[1] 22 24 26 28…

M. Beausoleil
- 3,141
- 6
- 29
- 61
16
votes
3 answers
Using lapply to change column names of a list of data frames
I'm trying to use lapply on a list of data frames; but failing at passing the parameters correctly (I think).
List of data frames:
df1 <- data.frame(A = 1:10, B= 11:20)
df2 <- data.frame(A = 21:30, B = 31:40)
listDF <- list(df1, df2,df3) …

user3310782
- 811
- 2
- 10
- 18
16
votes
2 answers
How to apply mean function over elements of a list in R
I have a list and I want to use lapply() to compute the mean of its elements. For example, for the seventh item of the list I have:
>list[[7]]
[1] 1 1 1 1 1 1 1 1 1 1
and my output should be:
> mean(temp[[7]][1:10])
[1] 1
But when I use lapply()…

hora
- 845
- 5
- 14
- 25
15
votes
5 answers
Dataframes in a list; adding a new variable with name of dataframe
I have a list of dataframes which I eventually want to merge while maintaining a record of their original dataframe name or list index. This will allow me to subset etc across all the rows. To accomplish this I would like to add a new variable 'id'…

Look Left
- 1,305
- 3
- 15
- 20
15
votes
4 answers
calculate median from data.table columns in R
I am trying to calculate a median value across a number of columns, however my data is a bit funky. It looks like the following example.
library(data.table)
dt <- data.table("ID" = c(1,2,3,4),"none" = c(0,5,5,3),
"ten" =…

Dan
- 2,625
- 5
- 27
- 42