[R] create n suffixes of length 1:n from string of length n

David Winsemius dwinsemius at comcast.net
Wed Oct 19 18:45:51 CEST 2016


> On Oct 19, 2016, at 9:41 AM, Jeff Newmiller <jdnewmil at dcn.davis.ca.us> wrote:
> 
> When I replace length with nchar, it works fine for me without mapply.
> 
> substr( rep( s, nchar(s) ), 1, seq.int( nchar(s) ) )

I failed to make the second `nchar` -> `length` substitution. It now works for me as well.

-- 
David

> -- 
> Sent from my phone. Please excuse my brevity.
> 
> On October 19, 2016 9:36:25 AM PDT, David Winsemius <dwinsemius at comcast.net> wrote:
>> 
>>> On Oct 19, 2016, at 8:44 AM, Jeff Newmiller
>> <jdnewmil at dcn.davis.ca.us> wrote:
>>> 
>>> These don't look like "suffixes" to me,  but whatever.
>>> 
>>> s <- "abc"
>>> substr( rep( s, length(s) ), 1, seq.int( length(s) ) )
>> 
>> I suspect that `nchar` was meant instead of `length` but it still
>> failed. How about:
>> 
>> lets <- paste0(letters,collapse=TRUE)
>> unname( mapply( substr, lets, 1, 1:26) )  # mapply will recycle 
> 

David Winsemius
Alameda, CA, USA



More information about the R-help mailing list