2
votes

I have to two types of datasets that I want to load, process using different functions and then join together.

I have multiple datasets of each of these types, which which names I store in a nested list. I want to apply two different functions (or one function that identifies each dataset in the nested list) to each of the sub-elements in my nested list.

I've tried multiple different variations of lapply, sapply and mapply but haven't succeeded. Instead of showing each thing I've tried, I'll emphasis the intuition of what I'm trying to do below:

list_of_comparisons = list(
  list_of_comparison1 = list(
   "1. Data/1. X data/Week 22",
   "1. Data/1. Y data/Week 22"),
  list_of_comparison2 = list(
   "1. Data/1. X data/Week 25",
   "1. Data/1. Y data/Week 25"
  )
)

X = function(First.Element.of.List) {
# Do Something 
}
Y = function(Second.Element.of.List) {
# Do Something else
}

final = left_join(X,Y, by = "ID") 

I'm searching for a wrapper function or some way which I essentially "loop" over each element, apply function X to the first sub-element, apply function Y to the second sub-element, store the dataframes in a final df, and then move on to the next list. Thus, preferably using lapply since i'll be able to use do.call(rbind) on the final list created by the lapply function.

1
Could you be more specific about the desired manipulation(s)? apply function XNelsonGon
I can do the manipulations that I want using the X and Y functions by loading the "1. Data/1. X data/Week 22", and "1. Data/1. Y data/Week 22" into the respective functions and then join them together using left_join. However, I have 50+ comparisons, and more coming. Thus, I'm looking for a solution where I can simply write a nested list and apply the two functions to each sub-list. I would like to use some version of lapply, as the results are stored in lists, where its easy to join into one df using do.call and rbindrandom_user_567
I think you might want map_at or modify_at from the purrr packageBen G

1 Answers

1
votes

OK, so here's an example with some numeric data that may make things easier to understand. Let's say you had a nested list of two lists of numbers like so:

double_list <- list(list(1, 2, 3), list(4, 5, 6))

[[1]]
[[1]][[1]]
[1] 1

[[1]][[2]]
[1] 2

[[1]][[3]]
[1] 3


[[2]]
[[2]][[1]]
[1] 4

[[2]][[2]]
[1] 5

[[2]][[3]]
[1] 6

And you wanted to perform a function (say multiply by 2) on the first object of each list, you could pass map_at from the purrr package to map (also from purrr) like so:

map(.x = double_list, .f = map_at, .at = 1, ~.*2)

[[1]]
[[1]][[1]]
[1] 2

[[1]][[2]]
[1] 2

[[1]][[3]]
[1] 3


[[2]]
[[2]][[1]]
[1] 8

[[2]][[2]]
[1] 5

[[2]][[3]]
[1] 6

You can of course use any function in the map call, doesn't have to be simple arithmetic.