# 字串替換
sub("_","","123_456_789") # 替換第一個出現的_ 為無空白
result:
[1] "123456_789"
gsub("_","","123_456_789") # 替換所有出現的_ 為無空白
result:
[1] "123456789"
# 資料擷取(grep)
plant = c("apple","berry","grape") # 設一筆字元型態資料
grep('e',plant) # 找出含有e的字串
[1] 1 2 3
● v= c(3,3,3,3,3,3,5,5,5,2,2,2,1,1,1)
table(v) # 計算每個數字出現幾次
result:
v
1 2 3 5 (值)
3 3 6 3 (次)
# 資料擷取(grep)
table(grepl('e',plant))
TRUE
3
# 資料擷取(grepl)
table(grepl('p',plant))
FALSE TRUE
1 2
● library(stringr)
nchar("Hello World") # 字元數
result:
[1] 11
#-------------------------------------------------------------------------
result:
[1] "Hello Wo"
#-------------------------------------------------------------------------
paste("Hello","World") # 合併字串
result:
[1] "Hello World"
#-------------------------------------------------------------------------
result:
[1] "HelloWorld"
#-------------------------------------------------------------------------
str_trim("Hello ") # 去掉空白
result:
[1] "Hello"
沒有留言:
張貼留言