trim: Remove leading and trailing spaces from character strings

Description Usage Arguments Details Value Author(s) See Also Examples

View source: R/trim.R

Description

Remove leading and trailing spaces from character strings and other related objects.

Usage

1
trim(s, recode.factor=TRUE, ...)

Arguments

s

object to be processed

recode.factor

should levels of a factor be recoded, see below

...

arguments passed to other methods, currently only to reorder.factor for factors

Details

trim is a generic function, where default method does nothing, while method for character s trims its elements and method for factor s trims levels. There are also methods for list and data.frame.

Trimming character strings can change the sort order in some locales. For factors, this can affect the coding of levels. By default, factor levels are recoded to match the trimmed sort order, but this can be disabled by setting recode.factor=FALSE. Recoding is done with reorder.factor.

Value

s with all leading and trailing spaces removed in its elements.

Author(s)

Gregory R. Warnes greg@warnes.net with contributions by Gregor Gorjanc

See Also

trimws, sub,gsub as well as argument strip.white in read.table and reorder.factor

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
s <- "    this is an example string    "
trim(s)

f <- factor(c(s, s, " A", " B ", "  C ", "D "))
levels(f)

trim(f)
levels(trim(f))

trim(f, recode.factor=FALSE)
levels(trim(f, recode.factor=FALSE))

l <- list(s=rep(s, times=6), f=f, i=1:6)
trim(l)

df <- as.data.frame(l)
trim(df)

Example output

sh: 1: cannot create /dev/null: Permission denied
gdata: Unable to locate valid perl interpreter
gdata: 
gdata: read.xls() will be unable to read Excel XLS and XLSX files
gdata: unless the 'perl=' argument is used to specify the location of a
gdata: valid perl intrpreter.
gdata: 
gdata: (To avoid display of this message in the future, please ensure
gdata: perl is installed and available on the executable search path.)
sh: 1: cannot create /dev/null: Permission denied
gdata: Unable to load perl libaries needed by read.xls()
gdata: to support 'XLX' (Excel 97-2004) files.

gdata: Unable to load perl libaries needed by read.xls()
gdata: to support 'XLSX' (Excel 2007+) files.

gdata: Run the function 'installXLSXsupport()'
gdata: to automatically download and install the perl
gdata: libaries needed to support Excel XLS and XLSX formats.

Attaching package: 'gdata'

The following object is masked from 'package:stats':

    nobs

The following object is masked from 'package:utils':

    object.size

The following object is masked from 'package:base':

    startsWith

[1] "this is an example string"
[1] "    this is an example string    " "  C "                             
[3] " A"                                " B "                              
[5] "D "                               
[1] this is an example string this is an example string
[3] A                         B                        
[5] C                         D                        
Levels: A B C D this is an example string
[1] "A"                         "B"                        
[3] "C"                         "D"                        
[5] "this is an example string"
[1] this is an example string this is an example string
[3] A                         B                        
[5] C                         D                        
Levels: this is an example string C A B D
[1] "this is an example string" "C"                        
[3] "A"                         "B"                        
[5] "D"                        
$s
[1] "this is an example string" "this is an example string"
[3] "this is an example string" "this is an example string"
[5] "this is an example string" "this is an example string"

$f
[1] this is an example string this is an example string
[3] A                         B                        
[5] C                         D                        
Levels: A B C D this is an example string

$i
[1] 1 2 3 4 5 6

                          s                         f i
1 this is an example string this is an example string 1
2 this is an example string this is an example string 2
3 this is an example string                         A 3
4 this is an example string                         B 4
5 this is an example string                         C 5
6 this is an example string                         D 6

gdata documentation built on May 2, 2019, 8:49 a.m.