# listr of car names
car_names <- strsplit(row.names(mtcars)[1:5], " ")
sapply(car_names, get_1st)
# [1] "Mazda" "Mazda" "Datsun" "Hornet" "Hornet"
sapply(car_names, get_nth, 2)
# [1] "RX4" "RX4" "710" "4" "Sportabout"
# OR if you just want to pull a simple string apart (e.g. someone's full name):
get_1st_word(rownames(mtcars)[1:5])
#[1] "Mazda" "Mazda" "Datsun" "Hornet" "Hornet"
get_last_word(rownames(mtcars)[1:5])
#[1] "RX4" "Wag" "710" "Drive" "Sportabout"
get_nth_word(rownames(mtcars)[1:5], 2)
#[1] "RX4" "RX4" "710" "4" "Sportabout"
my_stuff <- c(1:10, 10, 5)
# These are straight forward
get_1st(my_stuff)
get_nth(my_stuff, 3)
get_last(my_stuff)
get_most_frequent(my_stuff)
my_chars <- c("a", "b", "b", "a", "g", "o", "l", "d")
get_most_frequent(my_chars)
get_most_frequent(my_chars, collapse = " & ")
generic_string <- "Who's A good boy? Winston's a good boy!"
get_1st_word(generic_string)
get_nth_word(generic_string, 3)
get_last_word(generic_string)
# default ignores case and punctuation
get_most_frequent_word(generic_string)
# can change like so:
get_most_frequent_word(generic_string, ignore.case = FALSE, ignore.punct = FALSE)
Run the code above in your browser using DataLab