Adding differing chars to list of dataframes via lapply

apply function to multiple data frames r
lapply select columns
r lapply custom function
mapply
r apply function with multiple arguments to data frame
r apply dataframe
lapply list of dataframes
r apply function to each element of dataframe

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),
                  "two" = sample(1:10,10,replace=TRUE))

df2 <- data.frame("three" = sample(1:10,10,replace=TRUE),
                  "four" = sample(1:10,10,replace=TRUE))

df3 <- data.frame("five" = sample(1:10,10,replace=TRUE),
                  "six" = sample(1:10,10,replace=TRUE))

# Combine them to list 
dflist = list("df1"=df1,"df2"=df2,"df3"=df3)

# add labelling column 
dflist$df1["label"] <- "a"
dflist$df2["label"] <- "b"
dflist$df3["label"] <- "c"

# With lapply I can only add either "a", "b" or "c" 
dflist = list("df1"=df1,"df2"=df2,"df3"=df3)
labvec <- c("a","b","c")
lapply(dflist,function(x) cbind(x,labvec[2])) # I have to select 1, 2 or 3.

Asked differently: Could i also index over "labvec" with lapply?


Data Frames and Apply, Three ways to index vectors, matrices, data frames, lists: integers, Booleans, names switch() : shortcut for using if() , elseif() , and else in combination my. df$chars # Note: this one has been converted into a factor data type with a matrix, and add columns (of different data types) to make it a data frame When your data is in the form of a list, and you want to perform calculations on each element of that list in R, the appropriate apply function is lapply(). For example, to get the class of each element of iris, do the following:


A solution with lapply() and the use of dplyr::mutate().

library(dplyr)

dflist <- lapply(1:length(dflist), function(i) {
  dflist[[i]] %>% 
    mutate(label = letters[i])
})
# lapply(dflist, head, 2)
# [[1]]
#   one two label
# 1   3   3     a
# 2   4   2     a
# 
# [[2]]
#   three four label
# 1    10    5     b
# 2     3    6     b
# 
# [[3]]
#   five six label
# 1    9   5     c
# 2    7   9     c

Note that this is just "forcing" the lapply(), I mean, it's basically a for loop not that well hidden.

How to apply a plotting function to a list of dataframes *and* a , Hi, I want to split a dataframe in multiple dataframes, apply a plotting function object, and finally create a patchworked plot by adding the plots together. in the list msleep_by_vore , with corresponding title from the character vector titles . Just a request: can we keep using msleep as a sample dataset? In short, there is an implicit for loop that gets written for you. You can use lapply() to iterate over anything: a list, a dataframe (which is just a special type of list) a vector of numbers, a vector of characters…..whatever. In our case, the variables of interest are stored in columns 3 through 8 of our data frame.


Using tidyverse with map2

library(tidyverse)
map2(dflist, labvec, ~ .x %>% 
                      mutate(label = .y))
#$df1
#   one two label
#1    3   3     a
#2    4   2     a
#3    6   7     a
#4   10   4     a
#5    3   8     a
#6    9   5     a
#7   10   8     a
#8    7  10     a
#9    7   4     a
#10   1   8     a

#$df2
#   three four label
#1     10    5     b
#2      3    6     b
#3      7    5     b
#4      2    2     b
#5      3    9     b
#6      4    7     b
#7      1    8     b
#8      4    2     b
#9      9    8     b
#10     4    5     b

#$df3
#   five six label
#1     9   5     c
#2     7   9     c
#3     8   5     c
#4     6   3     c
#5     6   1     c
#6     8   1     c
#7     1   4     c
#8     5   6     c
#9     8   7     c
#10    7   5     c

R Statistical Application Development by Example Beginner's Guide, Length 1 2 1o_ Different ways of extracting objects from a dataframe object with different structures, one being an integer and the other one a character vector. Using the data. frame function we created a new object olfl, which contains both class. lapply is a useful utility in R which applies a function over a list or vector  lapply returns a list of the same length as X, each element of which is the result of applying FUN to the corresponding element of X. sapply is a user-friendly version and wrapper of lapply by default returning a vector, matrix or, if simplify = "array", an array if appropriate, by applying simplify2array(). sapply(x, f, simplify = FALSE, USE.NAMES = FALSE) is the same as lapply(x, f). vapply


Statistical Application Development with R and Python, Here, we first created two vectors of the same length with different structures, one being an integer and the other one a character vector. Using the data.frame function, we created a new object df1, which contains both the vectors. sapply and class. lapply is a useful utility in R that applies a function over a list or vector and  lapply(X, FUN) Arguments: -X: A vector or an object -FUN: Function applied to each element of x l in lapply() stands for list. The difference between lapply() and apply() lies between the output return. The output of lapply() is a list. lapply() can be used for other objects like data frames and lists. lapply() function does not need MARGIN.


lapply: Apply a Function over a List or Vector, lapply returns a list of the same length as X, each element of which is the result of logical or character string; should the result be simplified to a vector, matrix or error message is given if arguments named X or FUN are passed through . the Names of All Built-in Objects by: Apply a Function to a Data Frame Split by  My first attempt was to use two imbricated "lapply" functions running something like this: list_structure<-lapply(levels(A) function(x) { as.character(x) = lapply( levels(B), function(y) { as.character(y) = C[A==x & B==y] }) }) The real code was not quite as simple, but I managed to have it work, and it worked well on my first dataset (where A


rapply: Recursively Apply a Function to a List, rapply is a recursive version of lapply with flexibility in how the result is structured (how = ". a function of one “principal” argument, passing further arguments via . .. . classes. character vector of class names, or "ANY" to match any class. The semantics differ in detail from lapply : in particular the arguments are evaluated  If you use the rbindlist function in data.table, I believe you need to specify your preference using the fill argument. However, in this case, I don’t have a list of dataframes, I have a list of file names. This brings us to the lapply function, which allows the user to apply a function to each item in a list.