在 R 编程中将字符串修剪到指定的显示宽度 – strtrim()函数
R 语言中的strtrim()
函数用于将字符串修剪到指定的显示宽度。
Syntax: strtrim(x, width)
Parameters:
x: character vector
width: specified width
示例 1:
# R program to trim a string
# to specified width
# Creating a vector
x1 <- "GeeksforGeeks"
x2 <- "R Programming"
# Calling strtrim() function
strtrim(x1, 5)
strtrim(x2, 7)
输出:
[1] "Geeks"
[1] "R Progr"
示例 2:
# R program to trim a string
# to specified width
# Creating a vector
x <- c("Hello", "Geeks", "GFG")
# Calling strtrim() function
strtrim(x, 2)
strtrim(x, c(1, 2, 3))
输出:
[1] "He" "Ge" "GF"
[1] "H" "Ge" "GFG"