You are on page 1of 3

字串建立與輸出

 paste(..., sep = " ", collapse = NULL)


 將相同資料型態 objects 組成字串,預設字串間隔為空格
 paste("Apple", "Cherry", "Orange") paste("Apple", "Cherry", "Orange", sep = "&")
 paste(c("Apple", "Cherry", "Orange"), c("Red", "Blue", "Green")) # 向量化運算
 paste(c("Apple", "Cherry"), "Green", sep = "#")
 paste(c("Apple", "Cherry", "Orange"), c("Red", "Blue")) # 向量元素可重複使用
 paste("Apple", "Cherry", "Orange") vs. paste(c("Apple", "Cherry", "Orange"))
 paste(c("Apple", "Cherry"), "Green", sep = "#", collapse = "&") # 注意字串長度變化
 paste(LETTERS[1:10], collapse = "")
 paste(1:5) paste("std", 1:5, sep = "_") # 數字轉成字串
 sprintf(fmt, …)
 將不同資料型態 objects 組成字串,用法同 C 語言中的 printf
 sprintf("We study %s and %s", "AI", "Big Data")
 x <- 3 y <- 5.5 sprintf("%d + %f = %f", x, y, x+y)
 x <- 5:1 y <- LETTERS[6:10] sprintf("Num %d Letter %s", x, y) # 向量化運算
 z <- c("Y", "Z") sprintf("Num %d Letter %s", x, z) # 向量元素不能重複使用
 x <- c(5, 8, 10, 15) sprintf("Num_%d", x)
sprintf("Num_%2d", x) sprintf("Num_%02d", x) # 固定字串長度
 print(x)
 直接輸出單一 object 內容,不需定義格式
 print("Apple") print(10.5)
 print(c("Apple", "Cherry", "Pear")) print(seq(10, 20, by = 2))
 print(matrix(1:12, nrow = 3))
 print(data.frame(x = sample(5), y = LETTERS[6:10], z = round(runif(5, 2, 10), 2)))
 print(list(10, c("Apple", "Cherry"), matrix(10:15, nrow = 2)))
 cat(..., file = "", sep = " ")
 將不同資料型態 objects 串接成字串後輸出,預設字串間隔為空格
 無法輸出 data frame 和 list
 預設 file = "" 輸出至螢幕,留意 print 和 cat 在輸出格式的差別
 設定 file = "filename" 可寫入檔案,引數 append 預設為 FALSE (覆寫)
 cat("Apple") cat(10.5) cat(c("Apple", "Cherry", "Pear")) cat(seq(10, 20, by = 2))
 cat(matrix(1:12, nrow = 3))
 x <- c(3, 5, 8) cat("Apple", 10.5, x, "Cherry", sep = " && ")
 cat("Apple", 10.5, x, "\n","Cherry", sep = " && ") # "\n"為 new line
 cat(data.frame(x = sample(5), y = LETTERS[6:10], z = round(runif(5, 2, 10), 2)))
 cat(list(10, c("Apple", "Cherry"), matrix(10:15, nrow = 2)))

鍵盤輸入
1
 scan("", what = double(), n = -1, sep = "", quiet = FALSE)
 file = "":由鍵盤輸入
 what = double():輸入之資料型式,預設為 numeric,可為 ""、integer、logical、list 等
 n = -1:輸入 items 個數,預設為無限大,直到輸入 enter 鍵為止
 sep = "":除了 enter 之外,可以用空白鍵分隔 items
 quiet = FALSE:最後呈現輸入多少 items
 x <- scan("", n = 3, sep = " ", quiet = TRUE)
y <- scan("", what = "", sep = ",", quiet = TRUE) # 輸入內容轉成字串
z <- scan("", what = list(name = "", pay = integer(0), sex = ""))

字串處理
 查尋字串內容
 grep(pattern, x) 查尋字串
 pattern:查尋項目,可為字串或正則表達式 (regular expression)
 x:被查尋項目
 s1 <- replicate(5, paste(sample(letters[1:10], 8, replace = TRUE), collapse = ""))
grep("a", s1)
 grepl(pattern, x) 查尋是否含有特定字串
 grep("a", s1) grepl("a", s1)
 擷取字串內容
 substr(x, start, stop) 取出指定位置的字串
 x:character vector
 start/stop:開始/結束字元數
 s2 <- paste(LETTERS, collapse = "") s3 <- paste(letters, collapse = "")
s4 <- paste(0:9, collapse = "")
substr(s2, 3, 10) substr(c(s2, s3, s4), 3, 10) substr(c(s2, s3, s4), 1:3, 10:8)
substr(s4, 5, 12)
 substring(x, first) 取出指定位置到結束的字串
 first:開始字元數
 substring(s2, 5) substring(c(s2, s3, s4), 5) substring(c(s2, s3, s4), c(10, 15, 5))
substring(s4, 12)
 strtrim(x, width) (從開頭) 取出指定長度的字串
 strtrim(s2, 5) strtrim(c(s2, s3, s4), 5) strtrim(c(s2, s3, s4), 5:7)
strtrim(s4, 12)
 strsplit(x, split) 根據字元分割字串
 split:分隔字元
 回傳值為 list 型式
 strsplit(s1[1], "a") strsplit(s1, "a") strsplit(s1, letters[1:5])
 替換字串
2
 sub(old, new, x) 將 x 中的 old 字串替換成 new 字串
 old 可為正則表達式
 只執行一次替換
 sub("a", "#A#", s1[1]) sub("a", "#A#", s1)
 gsub(old, new, x)
 替換所有符合條件
 gsub("a", "#A#", s1[1]) gsub("a", "#A#", s1)

You might also like