1 and only the first element will be used" mean? Here is my dataset: FullName <- c("Jimmy John C...">

What does the error "condition has a length> 1 and only the first element will be used" mean?

Here is my dataset:

FullName <- c("Jimmy John Cephus", "Frank Chester", "Hank Chester", "Brody Buck Clyde", "Merle Rufus Roscoe Jed Quaid")
df <- data.frame(FullName)

Purpose: Look in FullName for any spaces, "" and extract FirstName.

My first step is to use the stringr library, because I will use the str_count () and word () functions.

Next, I test stringr::str_count(df$FullName, " ")against df and return R:

[1] 2 1 1 2 4

This is what I expect.

Next, I test the word () function:

stringr::word(df$FullName, 1)

R returns:

[1] "Jimmy" "Frank" "Hank"  "Brody" "Merle"

Again, this is what I expect.

Next, I create a simple UDF (user-defined function) that includes the str_count () function:

split_firstname = function(full_name){
  x <- stringr::str_count(full_name, " ")
  return(x)
}
split_firstname(df$FullName)

Again, R provides what I expect:

[1] 2 1 1 2 4

As a last step, I include the word () function in UDF and the code for all conditions:

    split_firstname = function(full_name){
  x <- stringr::str_count(full_name, " ")
  if(x==1){
    return(stringr::word(full_name,1))
  }else if(x==2){
    return(paste(stringr::word(full_name,1), stringr::word(full_name,2), sep = " "))
  }else if(x==4){
    return(paste(stringr::word(full_name,1), stringr::word(full_name,2), stringr::word(full_name,3), stringr::word(full_name,4), sep = " "))
  }
}

Then I call UDF and pass it FullName from df:

split_firstname(df$FullName)

This time I did NOT get what I expected, R came back:

[1] "Jimmy John"    "Frank Chester" "Hank Chester"  "Brody Buck"    "Merle Rufus"  
Warning messages:
1: In if (x == 1) { :
  the condition has length > 1 and only the first element will be used
2: In if (x == 2) { :
  the condition has length > 1 and only the first element will be used

, R :

"Jimmy John", "Frank", "Hank", "Brody Buck", "Merle Rufus Roscoe Jed"
+6
2

, if . , . case_when 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 = " ")
  )
}
+5

lukeA - , , , sapply from base-r rollise from dplyr

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
+2

Source: https://habr.com/ru/post/1688455/


All Articles