What does the error "the condition has length > 1 and only the first element will be used" mean?
the problem is that you are using an if-statement with a vector. This is not allowed and doesn't work as you would expect. You can use the case_when
function from dplyr
.
library(dplyr)
split_firstname <- function(full_name){
x <- stringr::str_count(full_name, " ")
case_when(
x == 1 ~ stringr::word(full_name, 1),
x == 2 ~ paste(stringr::word(full_name,1), stringr::word(full_name,2), sep = " "),
x == 4 ~ paste(stringr::word(full_name,1), stringr::word(full_name,2), stringr::word(full_name,3), stringr::word(full_name,4), sep = " ")
)
}
lukeA's answer is the best approach, but if you find you are unable to vectorise functions, sapply from base-r and rowwise from dplyr can solve this problem too
df$first <- sapply(df$FullName, split_firstname)
head(df)
FullName first
1 Jimmy John Cephus Jimmy John
2 Frank Chester Frank
3 Hank Chester Hank
4 Brody Buck Clyde Brody Buck
5 Merle Rufus Roscoe Jed Quaid Merle Rufus Roscoe Jed
library(dplyr)
df <- df %>% rowwise() %>%
mutate(split2 = split_firstname(FullName))
head(df)
FullName first split2
<fctr> <chr> <chr>
1 Jimmy John Cephus Jimmy John Jimmy John
2 Frank Chester Frank Frank
3 Hank Chester Hank Hank
4 Brody Buck Clyde Brody Buck Brody Buck
5 Merle Rufus Roscoe Jed Quaid Merle Rufus Roscoe Jed Merle Rufus Roscoe Jed