napply: napply: lapply with access to the names of the list argument

View source: R/capply.R

napplyR Documentation

napply: lapply with access to the names of the list argument

Description

lapply allows a function to operate on each element of a list but does not provide a way to access the name of the elements in the list. napply provides a mechanism for doing this by setting a ".name" attribute for each element. This attribute can be extracted with the .name function.

Usage

napply(X, FUN, ..., DO.CLEAN = TRUE)

Arguments

X

a list to be passed to lapply

FUN

a function to be passed to lapply

...

optional arguments to FUN

DO.CLEAN

remove ,name attribute in result

Details

WARNING: This sets and uses a '.name' attribute and is likely to conflict with functions that use such an attribute or that rely on 'is.vector'.

Examples

## Not run: 
# A common application occurs in manipulating lists of data frames
# (such as might have been read in as a list of similarly
# structured Excel sheets, each for a different year) 
# where the name of the data frame needs to be added as a variable
# to each data frame before merging or 'rbind'ing the data frames.

dlist <- list(
    "2019" = data.frame(x=1:3, y = 1:3 ),
    "2020" = data.frame(x=11:13, y = 11:13 )
)
dlist <- napply( dlist,
   function(d) {
      d$year <- as.numeric(.name(d))
      d
   })
dmerged <- do.call(rbind, dlist)    
dmerged

## End(Not run)

gmonette/spida2 documentation built on Aug. 11, 2024, 7:52 p.m.