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
240
votes
5 answers
Why use purrr::map instead of lapply?
Is there any reason why I should use
map(, function(x) )
instead of
lapply(, function(x) )
the output should be the same and the benchmarks I made seem to show that lapply is slightly faster…

Tim
- 7,075
- 6
- 29
- 58
212
votes
12 answers
Access lapply index names inside FUN
Is there a way to get the list index name in my lapply() function?
n = names(mylist)
lapply(mylist, function(list.elem) { cat("What is the name of this list element?\n" })
I asked before if it's possible to preserve the index names in the lapply()…

Robert Kubrick
- 8,413
- 13
- 59
- 91
148
votes
4 answers
passing several arguments to FUN of lapply (and others *apply)
I have a question regarding passing multiple arguments to a function, when using lapply in R.
When I use lapply with the syntax of lapply(input, myfun); - this is easily understandable, and I can define myfun like that:
myfun <- function(x) {
#…

Vasily A
- 8,256
- 10
- 42
- 76
106
votes
3 answers
Read all files in a folder and apply a function to each data frame
I am doing a relatively simple piece of analysis that I have put into a function on all the files in a particular folder. I was wondering whether anyone had any tips to help me automate the process on a number of different folders.
Firstly, I was…

KT_1
- 8,194
- 15
- 56
- 68
80
votes
6 answers
Access and preserve list names in lapply function
I need to access list names inside the lapply function. I've found some threads online where it's said I should iterate through the names of the list to be able to fetch each list element name in my function:
> n = names(mylist)
> mynewlist =…

Robert Kubrick
- 8,413
- 13
- 59
- 91
63
votes
3 answers
Creating a named list from two vectors (names, values)
Is there a way to use mapply on two vectors to construct a named list? The first vector would be of type character and contain the names used for the list while the second contains the values.
So far, the only solution I have is:
> dummyList =…

Jon Claus
- 2,862
- 4
- 22
- 33
52
votes
4 answers
Faster way to read fixed-width files
I work with a lot of fixed width files (i.e., no separating character) that I need to read into R. So, there is usually a definition of the column width to parse the string into variables. I can use read.fwf to read in the data without a problem. …

Mark Danese
- 2,331
- 1
- 20
- 25
47
votes
4 answers
How to tell lapply to ignore an error and process the next thing in the list?
I have an example function below that reads in a date as a string and returns it as a date object. If it reads a string that it cannot convert to a date, it returns an error.
testFunction <- function (date_in) {
return(as.Date(date_in))
…

John
- 5,139
- 19
- 57
- 62
46
votes
11 answers
Read multiple CSV files into separate data frames
Suppose we have files file1.csv, file2.csv, ... , and file100.csv in directory C:\R\Data and we want to read them all into separate data frames (e.g. file1, file2, ... , and file100).
The reason for this is that, despite having similar names they…

Fred
- 1,833
- 3
- 24
- 29
43
votes
2 answers
lapply with "$" function
Let's say I have a list of data.frames
dflist <- list(data.frame(a=1:3), data.frame(b=10:12, a=4:6))
If i want to extract the first column from each item in the list, I can do
lapply(dflist, `[[`, 1)
# [[1]]
# [1] 1 2 3
#
# [[2]]
# [1] 10 11…

MrFlick
- 195,160
- 17
- 277
- 295
42
votes
3 answers
What are the performance differences between for-loops and the apply family of functions?
It is often said that one should prefer lapply over for loops.
There are some exception as for example Hadley Wickham points out in his Advance R book.
(http://adv-r.had.co.nz/Functionals.html) (Modifying in place, Recursion etc).
The following is…

Federico Manigrasso
- 1,130
- 1
- 7
- 11
39
votes
3 answers
Using lapply to apply a function over list of data frames and saving output to files with different names
I have a list of data frames and have given each element in the list (e.g. each data frame) a name:
e.g.
df1 <- data.frame(x = c(1:5), y = c(11:15))
df2 <- data.frame(x = c(1:5), y = c(11:15))
mylist <- list(A = df1, B = df2)
I have a…

user2414840
- 721
- 1
- 7
- 15
38
votes
5 answers
How do you read multiple .txt files into R?
I'm using R to visualize some data all of which is in .txt format. There are a few hundred files in a directory and I want to load it all into one table, in one shot.
Any help?
EDIT:
Listing the files is not a problem. But I am having trouble going…

Eric Brotto
- 53,471
- 32
- 129
- 174
38
votes
2 answers
Apply function on a subset of columns (.SDcols) whilst applying a different function on another column (within groups)
This is very similar to a question applying a common function to multiple columns of a data.table uning .SDcols answered thoroughly here.
The difference is that I would like to simultaneously apply a different function on another column which is not…

Matt Weller
- 2,684
- 2
- 21
- 30
30
votes
1 answer
How to index an element of a list object in R
I'm doing the following in order to import some txt tables and keep them as list:
# set working directory - the folder where all selection tables are stored
hypo_selections<-list.files() # change object name according to each…

Mohr
- 323
- 1
- 3
- 9