0

I wanted to split a column in R into two new columns. I came across the following code on the internet which worked quite well:

as.numeric(lapply(strsplit(data$Coord, split=","), "[", 1))

My question is, how exactly does this work, in particular the latter part of the function where we have the "[" and the 1. Would someone be able to explain to me what lapply is doing here exactly, would really like to understand.

The above result gives back the first portion of the split before the comma in the data$Coord vector.

Thanks

  • Chapters 5, 6, and 10 of Hadley Wickham's book [Advanced R](https://adv-r.hadley.nz/) (which is free online) will give you all the details. In short, everything that exists in R is an object and everything you do in R is a function. So subsetting with `[` is a function and can be passed as an argument to `lapply`. – DanY Jul 28 '18 at 19:51
  • Thank you. I also found this helpful ... https://stackoverflow.com/questions/19260951/using-square-bracket-as-a-function-for-lapply-in-r?noredirect=1&lq=1 – Andrzej Marek Szanda Jul 29 '18 at 07:00

0 Answers0