substr                 package:base                 R Documentation

_S_u_b_s_t_r_i_n_g_s _o_f _a _C_h_a_r_a_c_t_e_r _V_e_c_t_o_r

_D_e_s_c_r_i_p_t_i_o_n:

     Extract or replace substrings in a character vector.

_U_s_a_g_e:

     substr(x, start, stop)
     substring(text, first, last = 1000000L)
     substr(x, start, stop) <- value
     substring(text, first, last = 1000000L) <- value

_A_r_g_u_m_e_n_t_s:

 x, text: a character vector.

start, first: integer. The first element to be replaced.

stop, last: integer. The last element to be replaced.

   value: a character vector, recycled if necessary.

_D_e_t_a_i_l_s:

     'substring' is compatible with S, with 'first' and 'last' instead
     of 'start' and 'stop'. For vector arguments, it expands the
     arguments cyclically to the length of the longest _provided_ none
     are of zero length.

     When extracting, if 'start' is larger than the string length then
     '""' is returned.

     For the extraction functions, 'x' or 'text' will be converted to a
     character vector by 'as.character' if it is not already one.

     For the replacement functions, if 'start' is larger than the
     string length then no replacement is done.  If the portion to be
     replaced is longer than the replacement string, then only the
     portion the length of the string is replaced.

     If any argument is an 'NA' element, the corresponding element of
     the answer is 'NA'.

     Elements of the result will be have the encoding declared as that
     of the current locale (see 'Encoding' if the corresponding input
     had a declared encoding and the current locale is either Latin-1
     or UTF-8.

_V_a_l_u_e:

     For 'substr', a character vector of the same length and with the
     same attributes as 'x' (after possible coercion).

     For 'substring', a character vector of length the longest of the
     arguments.  This will have names taken from 'x' (if it has any
     after coercion, repeated as needed), and other attributes copied
     from 'x' if it is the longest of the arguments).

     Elements of 'x' with a declared encoding (see 'Encoding') will be
     returned with the same encoding.

_N_o_t_e:

     The S4 version of 'substring<-' ignores 'last'; this version does
     not.

     These functions are often used with 'nchar' to truncate a display.
      That does not really work (you want to limit the width, not the
     number of characters, so it would be better to use 'strtrim'), but
     at least make sure you use 'nchar(type="c")' (the default since R
     2.6.0).

_R_e_f_e_r_e_n_c_e_s:

     Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988) _The New S
     Language_. Wadsworth & Brooks/Cole. ('substring'.)

_S_e_e _A_l_s_o:

     'strsplit', 'paste', 'nchar'.

_E_x_a_m_p_l_e_s:

     substr("abcdef",2,4)
     substring("abcdef",1:6,1:6)
     ## strsplit is more efficient ...

     substr(rep("abcdef",4),1:4,4:5)
     x <- c("asfef", "qwerty", "yuiop[", "b", "stuff.blah.yech")
     substr(x, 2, 5)
     substring(x, 2, 4:6)

     substring(x, 2) <- c("..", "+++")
     x

