I could solve this using loops, but I am trying think in vectors so my code will be more R-esque.
I have a list of names. The format is firstname_lastname. I want to get out of this list a separate list with only the first names. I can't seem to get my mind around how to do this. Here's some example data:
t <- c("bob_smith","mary_jane","jose_chung","michael_marx","charlie_ivan") tsplit <- strsplit(t,"_")
which looks like this:
> tsplit [[1]] [1] "bob" "smith" [[2]] [1] "mary" "jane" [[3]] [1] "jose" "chung" [[4]] [1] "michael" "marx" [[5]] [1] "charlie" "ivan"
I could get out what I want using loops like this:
for (i in 1:length(tsplit)){ if (i==1) {t_out <- tsplit[[i]][1]} else{t_out <- append(t_out, tsplit[[i]][1])} }
which would give me this:
t_out [1] "bob" "mary" "jose" "michael" "charlie"
So how can I do this without loops?
The R Break statement is very useful to exit from any loop such as For, While, and Repeat. While executing these, if it finds the break statement inside them, it will stop executing the code and immediately exit from the loop.
The list() function is used to create lists in R programming. We can use the [[index]] function to select an element in a list.
And one more approach:
t <- c("bob_smith","mary_jane","jose_chung","michael_marx","charlie_ivan") pieces <- strsplit(t,"_") sapply(pieces, "[", 1)
In words, the last line extracts the first element of each component of the list and then simplifies it into a vector.
How does this work? Well, you need to realise an alternative way of writing x[1]
is "["(x, 1)
, i.e. there is a function called [
that does subsetting. The sapply
call applies calls this function once for each element of the original list, passing in two arguments, the list element and 1.
The advantage of this approach over the others is that you can extract multiple elements from the list without having to recompute the splits. For example, the last name would be sapply(pieces, "[", 2)
. Once you get used to this idiom, it's pretty easy to read.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With