點我下載:song_rank2.csv
setwd('/Users/carplee/Desktop/IT python')
r = read.csv('data/song_rank2.csv')
r
r$Artist
[1] "五月天 阿信" "魏嘉瑩, 魏如昀" "陳芳語 , 茄子蛋" "蕭敬騰, 馬佳"
[5] "吳汶芳 " "琳誼 Ring, 許富凱" "張語噥 " "Ray 黃霆睿"
[9] "飛兒樂團 " "摩登兄弟劉宇寧" "五月天 阿信" "五月天 阿信"
[13] "魏嘉瑩, 魏如昀" "陳芳語 , 茄子蛋"
rep(0,14)
[1] 0 0 0 0 0 0 0 0 0 0 0 0 0 0
rep(0, length(r$Artist))
[1] 0 0 0 0 0 0 0 0 0 0 0 0 0 0
n = rep(0, length(r$Artist))
## vector
Artist2 = n
install.packages("dplyr") # 下載"dplyr"套件
library(dplyr) # 載入dplyr
mutate(r, Artist2 = Artist2,.after='Artist') #.before也可以
Rank Hits Song Co Artist Artist2 Date
1 1 98318 青空未來 否 五月天 阿信 0 2021-07-28
2 2 74629 勇氣 是 魏嘉瑩, 魏如昀 0 2021-07-27
3 3 62531 沒什麼大不了 是 陳芳語 , 茄子蛋 0 2021-08-06
4 4 38971 我 是 蕭敬騰, 馬佳 0 2021-08-06
5 5 12342 人間遊戲 否 吳汶芳 0 2021-07-30
6 6 7023 愛情限時批 是 琳誼 Ring, 許富凱 0 2021-08-01
7 7 5382 故作完美 否 張語噥 0 2021-07-30
8 8 4999 我們 否 Ray 黃霆睿 0 2021-07-29
9 9 4823 愛在蔓延時 否 飛兒樂團 0 2021-07-20
10 10 4810 煙火星辰 否 摩登兄弟劉宇寧 0 2021-07-25
11 11 4333 紅空過去 否 五月天 阿信 0 2021-07-28
12 12 3204 放空現在 否 五月天 阿信 0 2021-07-04
13 13 2222 力量 是 魏嘉瑩, 魏如昀 0 2021-07-12
14 14 1083 很重要 是 陳芳語 , 茄子蛋 0 2021-08-09
Url
1 https://www.kkbox.com/tw/tc/song/d6L00KOUN.rUv3ePUv3eP0XL-index.html
2 https://www.kkbox.com/tw/tc/song/PLQ004POLLr78J1P78J1P0XL-index.html
3 https://www.kkbox.com/tw/tc/song/uX800A9O0.rHGjNHHGjNH0XL-index.html
4 https://www.kkbox.com/tw/tc/song/kZd00W8WP.rkDPo9kDPo90XL-index.html
5 https://www.kkbox.com/tw/tc/song/lFz00IJHaXrTQRJxTQRJx0XL-index.html
6 https://www.kkbox.com/tw/tc/song/Gpb00L-OOTrXdqOPXdqOP0XL-index.html
7 https://www.kkbox.com/tw/tc/song/ynD0062.8DrAtt8PAtt8P0XL-index.html
8 https://www.kkbox.com/tw/tc/song/ILP00xN8NXr3QHDL3QHDL0XL-index.html
9 https://www.kkbox.com/tw/tc/song/XQe00DOHa9rLNSB.LNSB.0XL-index.html
10 https://www.kkbox.com/tw/tc/song/7L400Gp-t.rPyye9Pyye90XL-index.html
11 https://www.kkbox.com/tw/tc/song/d6L00KOUN.rUv3ePUv3eP0XL-index.html
12 https://www.kkbox.com/tw/tc/song/d6L00KOUN.rUv3ePUv3eP0XL-index.html
13 https://www.kkbox.com/tw/tc/song/PLQ004POLLr78J1P78J1P0XL-index.html
14 https://www.kkbox.com/tw/tc/song/uX800A9O0.rHGjNHHGjNH0XL-index.html
r %>% mutate(Artist2 = Artist2,.after='Artist')
r = r %>% mutate(Artist2 = Artist2,.after='Artist')
r$Artist
[1] "五月天 阿信" "魏嘉瑩, 魏如昀" "陳芳語 , 茄子蛋" "蕭敬騰, 馬佳"
[5] "吳汶芳 " "琳誼 Ring, 許富凱" "張語噥 " "Ray 黃霆睿"
[9] "飛兒樂團 " "摩登兄弟劉宇寧" "五月天 阿信" "五月天 阿信"
[13] "魏嘉瑩, 魏如昀" "陳芳語 , 茄子蛋"
#
s = r$Artist[2]
strsplit(s, split=',')
# [[1]]
# [1] "魏嘉瑩" " 魏如昀"
strsplit(s, split=',')[[1]][1]
# [1] "魏嘉瑩"
strsplit(s, split=',')[[1]][2]
# [1] " 魏如昀"
install.packages("stringr")
library(stringr)
str_trim(" k")
[1] "k"
strsplit(s, split=',')[[1]][2] %>% str_trim()
[1] "魏如昀"
r$Artist
[1] "五月天 阿信" "魏嘉瑩, 魏如昀" "陳芳語 , 茄子蛋" "蕭敬騰, 馬佳"
[5] "吳汶芳 " "琳誼 Ring, 許富凱" "張語噥 " "Ray 黃霆睿"
[9] "飛兒樂團 " "摩登兄弟劉宇寧" "五月天 阿信" "五月天 阿信"
[13] "魏嘉瑩, 魏如昀" "陳芳語 , 茄子蛋"
strsplit(r$Artist, split=',')
[[1]]
[1] "五月天 阿信"
[[2]]
[1] "魏嘉瑩" " 魏如昀"
[[3]]
[1] "陳芳語 " " 茄子蛋"
[[4]]
[1] "蕭敬騰" " 馬佳"
[[5]]
[1] "吳汶芳 "
[[6]]
[1] "琳誼 Ring" " 許富凱"
[[7]]
[1] "張語噥 "
[[8]]
[1] "Ray 黃霆睿"
[[9]]
[1] "飛兒樂團 "
[[10]]
[1] "摩登兄弟劉宇寧"
[[11]]
[1] "五月天 阿信"
[[12]]
[1] "五月天 阿信"
[[13]]
[1] "魏嘉瑩" " 魏如昀"
[[14]]
[1] "陳芳語 " " 茄子蛋"
str_split_fixed(r$Artist, pattern=',',2)
[,1] [,2]
[1,] "五月天 阿信" ""
[2,] "魏嘉瑩" " 魏如昀"
[3,] "陳芳語 " " 茄子蛋"
[4,] "蕭敬騰" " 馬佳"
[5,] "吳汶芳 " ""
[6,] "琳誼 Ring" " 許富凱"
[7,] "張語噥 " ""
[8,] "Ray 黃霆睿" ""
[9,] "飛兒樂團 " ""
[10,] "摩登兄弟劉宇寧" ""
[11,] "五月天 阿信" ""
[12,] "五月天 阿信" ""
[13,] "魏嘉瑩" " 魏如昀"
[14,] "陳芳語 " " 茄子蛋"
str_split_fixed(r$Artist, pattern=',',2)[,1] %>% str_trim()
[1] "五月天 阿信" "魏嘉瑩" "陳芳語" "蕭敬騰"
[5] "吳汶芳" "琳誼 Ring" "張語噥" "Ray 黃霆睿"
[9] "飛兒樂團" "摩登兄弟劉宇寧" "五月天 阿信" "五月天 阿信"
[13] "魏嘉瑩" "陳芳語"
str_split_fixed(r$Artist, pattern=',',2)[,2] %>% str_trim()
[1] "" "魏如昀" "茄子蛋" "馬佳" "" "許富凱" "" "" ""
[10] "" "" "" "魏如昀" "茄子蛋"
art1 = str_split_fixed(r$Artist, pattern=',',2)[,1] %>% str_trim()
art2 = str_split_fixed(r$Artist, pattern=',',2)[,2] %>% str_trim()
r = r %>% mutate(art1=art1, art2=art2, .before="Artist")
r
r = within(r, rm(Artist2))
r