Liran
Liran

Reputation: 11

lapply with a split vector

I started off with the vector split_low

# Definition of split_low
pioneers <- c("GAUSS:1777", "BAYES:1702", "PASCAL:1623", "PEARSON:1857")
split <- strsplit(pioneers, split = ":")
split_low <- lapply(split, tolower)

Then I used lapply to select specific element from split_low vector:

select_el <- function(x, index) {
  x[index]
}


names <- lapply(split_low, select_el, 1)
years <- lapply(split_low, select_el, 2)

MY QUESTION is this: why does inputting names return a list of only the names (i.e. "GAUSS", "BAYES", etc) and not the names together with the years (i.e. "GAUSS 1777", "BAYES 1702", etc)?

After all, when I type in

split_low[1]

Or

select_el(split_low, 1]

R returns GAUSS 1777 rather than just GAUSS

How come lapply knows to separate the names from the years?

Upvotes: 0

Views: 287

Answers (1)

Melissa Key
Melissa Key

Reputation: 4551

To see what is going on, start by looking directly at split_low:

> split_low
[[1]]
[1] "gauss" "1777" 

[[2]]
[1] "bayes" "1702" 

[[3]]
[1] "pascal" "1623"  

[[4]]
[1] "pearson" "1857" 

Notice that the result is a list of 4 elements, and each element is a vector of 2 elements.

When we type split_low[1], this request the first element of split_low - which is the vector of two elements, gauss and 1777. (Technically, it is a list of length 1, containing this vector. To get at just the vector, use split_low[[1]].) In contrast, when you call your select_el function, you apply a function to each element of the list, and that function returns the nth element of the corresponding vector - in this case, the first index returns names, and the second returns years. Then, lapply creates a list out of these outputs - and hence the result of just names, or just years.

Upvotes: 2

Related Questions