我有一个数据框 df,其中有两列,一列是歌曲的标题,另一列是组合的标题和艺术家。我希望创建一个单独的艺术家字段。 这里展示了前三行:
title titleArtist
I'll Never Smile Again I'll Never Smile Again TOMMY DORSEY & HIS ORCHESTRA / FRANK SINATRA & PIED PIPERS
Imagination Imagination GLENN MILLER & HIS ORCHESTRA / RAY EBERLE
The Breeze And I The Breeze And I JIMMY DORSEY & HIS ORCHESTRA / BOB EBERLY
这组数据没有问题,这段代码也没有问题。
library(stringr)
library(dplyr)
df %>%
head(3) %>%
mutate(artist=str_to_title(str_trim(str_replace(titleArtist,title,"")))) %>%
select(artist,title)
artist title
1 Tommy Dorsey & His Orchestra / Frank Sinatra & Pied Pipers I'll Never Smile Again
2 Jimmy Dorsey & His Orchestra / Bob Eberly The Breeze And I
3 Glenn Miller & His Orchestra / Ray Eberle Imagination
但是当我将其应用于成千上万行时,就会出现错误。
Error: Incorrectly nested parentheses in regexp pattern. (U_REGEX_MISMATCHED_PAREN)
#or for part of the mutation
df$artist <-str_replace(df$titleArtist,df$title,"")
Error in stri_replace_first_regex(string, pattern, replacement, opts_regex = attr(pattern, :
Incorrectly nested parentheses in regexp pattern. (U_REGEX_MISMATCHED_PAREN)
我已从列中删除了所有括号,代码似乎可以工作一段时间,然后出现错误。
Error: Syntax error in regexp pattern. (U_REGEX_RULE_SYNTAX)
是不是还有其他特殊字符可能会导致问题,或者可能是其他原因呢?
谢谢您!
traceback()
有没有提供关于触发错误的任何信息? - dwwgsub
或sub
会与str_replace
抛出相同的错误吗?我看到你在titleArtist
中有/
,它也可能出现在title
中吗?没有数据访问权限真的很难分析这个问题。 - dwwifelse()
。 - Parfait