【发布时间】:2020-04-12 11:19:18
【问题描述】:
我的数据可以从这里下载
tuesdata <- tidytuesdayR::tt_load(2020, week = 4)
spotify <- tuesdata$spotify_songs
我希望 track_name 里面没有任何 () 和文本。
预期输出:如果我的曲目名称是 RITMO(Bad Boys For Life),我希望它只有 RITMO。
我们可以将 () 中的部分,即 Bad Boys For Life 在这种情况下放入一个单独的专栏专长中。我正在尝试使用以下方法,看到 10-20 SO 问题,但无法弄清楚。
spotify %>%
extract(., track_name, into = c("track_name2", "feat"), "(\\.+)\\s*(.*)",remove = F)
我知道我的正则表达式有问题,但不确定如何获得预期的输出
为 spotify 添加 dput
structure(list(track_id = c("6f807x0ima9a1j3VPbc7VN", "0r7CVbZTWZgbTCYdfa2P31",
"1z1Hg7Vb0AhHDiEmnDE79l", "75FpbthrwQmzHlBJLuGdC7", "1e8PAfcKUYoKkxPhrHqw4x",
"7fvUMiyapMsRRxr07cU8Ef"), track_name = c("I Don't Care (with Justin Bieber) - Loud Luxury Remix",
"Memories - Dillon Francis Remix", "All the Time - Don Diablo Remix",
"Call You Mine - Keanu Silva Remix", "Someone You Loved - Future Humans Remix",
"Beautiful People (feat. Khalid) - Jack Wins Remix"), track_artist = c("Ed Sheeran",
"Maroon 5", "Zara Larsson", "The Chainsmokers", "Lewis Capaldi",
"Ed Sheeran"), track_popularity = c(66, 67, 70, 60, 69, 67),
track_album_id = c("2oCs0DGTsRO98Gh5ZSl2Cx", "63rPSO264uRjW1X5E6cWv6",
"1HoSmj2eLcsrR0vE9gThr4", "1nqYsOef1yKKuGOVchbsk6", "7m7vv9wlQ4i0LFuJiE2zsQ",
"2yiy9cd2QktrNvWC2EUi0k"), track_album_name = c("I Don't Care (with Justin Bieber) [Loud Luxury Remix]",
"Memories (Dillon Francis Remix)", "All the Time (Don Diablo Remix)",
"Call You Mine - The Remixes", "Someone You Loved (Future Humans Remix)",
"Beautiful People (feat. Khalid) [Jack Wins Remix]"), track_album_release_date = c("2019-06-14",
"2019-12-13", "2019-07-05", "2019-07-19", "2019-03-05", "2019-07-11"
), playlist_name = c("Pop Remix", "Pop Remix", "Pop Remix",
"Pop Remix", "Pop Remix", "Pop Remix"), playlist_id = c("37i9dQZF1DXcZDD7cfEKhW",
"37i9dQZF1DXcZDD7cfEKhW", "37i9dQZF1DXcZDD7cfEKhW", "37i9dQZF1DXcZDD7cfEKhW",
"37i9dQZF1DXcZDD7cfEKhW", "37i9dQZF1DXcZDD7cfEKhW"), playlist_genre = c("pop",
"pop", "pop", "pop", "pop", "pop"), playlist_subgenre = c("dance pop",
"dance pop", "dance pop", "dance pop", "dance pop", "dance pop"
), danceability = c(0.748, 0.726, 0.675, 0.718, 0.65, 0.675
), energy = c(0.916, 0.815, 0.931, 0.93, 0.833, 0.919), key = c(6,
11, 1, 7, 1, 8), loudness = c(-2.634, -4.969, -3.432, -3.778,
-4.672, -5.385), mode = c(1, 1, 0, 1, 1, 1), speechiness = c(0.0583,
0.0373, 0.0742, 0.102, 0.0359, 0.127), acousticness = c(0.102,
0.0724, 0.0794, 0.0287, 0.0803, 0.0799), instrumentalness = c(0,
0.00421, 2.33e-05, 9.43e-06, 0, 0), liveness = c(0.0653,
0.357, 0.11, 0.204, 0.0833, 0.143), valence = c(0.518, 0.693,
0.613, 0.277, 0.725, 0.585), tempo = c(122.036, 99.972, 124.008,
121.956, 123.976, 124.982), duration_ms = c(194754, 162600,
176616, 169093, 189052, 163049)), class = c("tbl_df", "tbl",
"data.frame"), row.names = c(NA, -6L))
【问题讨论】:
-
这有点不清楚。试试
"^(.*?)(?:\\(([^()]*)\\).*)?$"。I Don't Care (with Justin Bieber) - Loud Luxury Remix或Beautiful People (feat. Khalid) - Jack Wins Remix怎么办? -
那么,
"^(.*?)(?:\\(([^()]*)\\).*)?$"是否按预期工作? -
不是每一列都有括号中的文本 (
()) ,在这种情况下你想返回什么。你能显示这 6 行的预期输出吗?可能,删除此问题不需要的其他列。 -
@RonakShah,对于这种情况,我只想返回显示的字符串。基本上我只是想消除一切如果有这个'('符号开始