[R] Extract letters from a column

SH emptican at gmail.com
Wed Mar 13 15:41:32 CET 2013


Thank you so much, Jorge and arun!!!  Both works well.

Steve

On Wed, Mar 13, 2013 at 10:26 AM, Jorge I Velez
<jorgeivanvelez at gmail.com> wrote:
> Try
>
> x <- c("Tom Cruiser", "Bread Pett", "Arnold Schwiezer")
> sapply(strsplit(x, " "), function(r) paste0(substr(r[1], 1, 3), substr(r[2],
> 1, 3)))
> [1] "TomCru" "BrePet" "ArnSch"
>
> HTH,
> Jorge.-
>
>
> On Thu, Mar 14, 2013 at 1:21 AM, SH <emptican at gmail.com> wrote:
>>
>> What I want to do is to extrac three letters from first and last name
>> and to combine them to make another column 'abb'.  The column 'abb' is
>> to be a my final product.  I can make column 'abb' using 'paste'
>> function once I have two parts from the first column 'name'.
>>
>> Thanks,
>>
>> Steve
>>
>> On Wed, Mar 13, 2013 at 10:17 AM, Jorge I Velez
>> <jorgeivanvelez at gmail.com> wrote:
>> > Try
>> >
>> > substr(tempdf$abb 4, 6)
>> >
>> > --JIV
>> >
>> >
>> >
>> > On Thu, Mar 14, 2013 at 1:15 AM, SH <emptican at gmail.com> wrote:
>> >>
>> >> Dear Jorge,
>> >>
>> >> I gave me this result (below) since it defines starting from the forth
>> >> letter and ending 6th letter from the first element.
>> >>
>> >> > substr(tempdf$name, 4, 6)
>> >> [1] " Cr" "ad " "old"
>> >>
>> >> I would like to have letters from first and second elements if
>> >> possible.
>> >>
>> >> Thanks for replying,
>> >>
>> >> Steve
>> >>
>> >>
>> >> On Wed, Mar 13, 2013 at 10:10 AM, Jorge I Velez
>> >> <jorgeivanvelez at gmail.com> wrote:
>> >> > Dear SH,
>> >> >
>> >> > Hmmm... what about
>> >> >
>> >> > substr(tempdf$name, 4, 6))
>> >> >
>> >> > ?
>> >> >
>> >> > HTH,
>> >> > Jorge.-
>> >> >
>> >> >
>> >> > On Thu, Mar 14, 2013 at 1:06 AM, SH <emptican at gmail.com> wrote:
>> >> >>
>> >> >> Dear list:
>> >> >>
>> >> >> I would like to extract three letters from first and second elements
>> >> >> in one column and make a new column.
>> >> >>
>> >> >> For example below,
>> >> >>
>> >> >> > tempdf = read.table("clipboard", header=T, sep='\t')
>> >> >> > tempdf
>> >> >>               name var1 var2    abb
>> >> >> 1      Tom Cruiser    1    6 TomCru
>> >> >> 2       Bread Pett    2    5 BrePet
>> >> >> 3 Arnold Schwiezer    3    7 ArnSch
>> >> >> > (p1 = substr(tempdf$name, 1, 3))
>> >> >> [1] "Tom" "Bre" "Arn"
>> >> >>
>> >> >> I was able to extract three letters from first name, however, I
>> >> >> don't
>> >> >> know how to extract three letters from last name (i.e., 'Cru',
>> >> >> 'Pet',
>> >> >> and 'Sch').  Can anyone give me a suggestion?  Many thanks in
>> >> >> advance.
>> >> >>
>> >> >> Best,
>> >> >>
>> >> >> Steve
>> >> >>
>> >> >> ______________________________________________
>> >> >> R-help at r-project.org mailing list
>> >> >> https://stat.ethz.ch/mailman/listinfo/r-help
>> >> >> PLEASE do read the posting guide
>> >> >> http://www.R-project.org/posting-guide.html
>> >> >> and provide commented, minimal, self-contained, reproducible code.
>> >> >
>> >> >
>> >
>> >
>
>



More information about the R-help mailing list