r语言 - 错误"the condition has length > 1 and only the first element will be used"是什么意思?



这是我的数据集:

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

目标: 查看全名中是否有任何空格",并提取名字。

我的第一步是使用stringr库,因为我将使用str_count()和word()函数。

接下来,我针对 df 和 R 返回值测试 stringr::str_count(df$FullName, " ")

[1] 2 1 1 2 4

这是我所期望的。

接下来我测试 word() 函数:

stringr::word(df$FullName, 1)

R 返回:

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

同样,这是我所期望的。

接下来,我构造一个简单的UDF(用户定义函数),其中包含str_count()函数:

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

同样,R 提供了我所期望的:

[1] 2 1 1 2 4

作为最后一步,我将 word() 函数合并到 UDF 中,并为所有条件编写代码:

    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 = " "))
  }
}

然后我调用 UDF 并将 df 的全名传递给它:

split_firstname(df$FullName)

这次我没有得到我期望的,R 返回:

[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"

问题是你正在使用带有向量的if语句。这是不允许的,并且不会按预期工作。您可以使用 dplyr 中的case_when功能。

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 的答案是最好的方法,但如果你发现你无法向量化函数,从 base-r 和 dplyr 的 rowwise 也可以解决这个问题

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

相关内容

最新更新