character comp

classic Classic list List threaded Threaded
9 messages Options
Reply | Threaded
Open this post in threaded view
|

character comp

Val-17
Hi  All,
In a given data frame I  want to compare character values of two columns.
My sample data looks like as follow,

mydataframe <- read.table( text='ID  var1 var2
  R1   AA  AAA
  R2   AAA AAA
  R3    A  AAAA
  R4   AA   A
  R5   A  AAA', header = TRUE, as.is = TRUE )

For each ID, I want  create the third column "dvar" as  difference
between var1 and var2
 Row1( R1)   the "dvar" value will be -1 and the complete  desired out
put looks like as follow.

 ID    var1 var2   dvar
 R1   AA    AAA    -1
 R2  AAA  AAA      0
 R3    A    AAAA    -3
 R4   AA       A        1
 R5    A     AAA      -2

How do i do this? Any help please?
Thank you

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.
Reply | Threaded
Open this post in threaded view
|

Re: character comp

Erin Hodgess-2
Will it always be A’s or will there be a mix please?

On Sat, Feb 9, 2019 at 11:06 AM Val <[hidden email]> wrote:

> Hi  All,
> In a given data frame I  want to compare character values of two columns.
> My sample data looks like as follow,
>
> mydataframe <- read.table( text='ID  var1 var2
>   R1   AA  AAA
>   R2   AAA AAA
>   R3    A  AAAA
>   R4   AA   A
>   R5   A  AAA', header = TRUE, as.is = TRUE )
>
> For each ID, I want  create the third column "dvar" as  difference
> between var1 and var2
>  Row1( R1)   the "dvar" value will be -1 and the complete  desired out
> put looks like as follow.
>
>  ID    var1 var2   dvar
>  R1   AA    AAA    -1
>  R2  AAA  AAA      0
>  R3    A    AAAA    -3
>  R4   AA       A        1
>  R5    A     AAA      -2
>
> How do i do this? Any help please?
> Thank you
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> 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.
>
--
Erin Hodgess, PhD
mailto: [hidden email]

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.
Reply | Threaded
Open this post in threaded view
|

Re: character comp

Val-17
Hi Erin,  Yes, it is always  A's.

On Sat, Feb 9, 2019 at 12:22 PM Erin Hodgess <[hidden email]> wrote:

>
> Will it always be A’s or will there be a mix please?
>
> On Sat, Feb 9, 2019 at 11:06 AM Val <[hidden email]> wrote:
>>
>> Hi  All,
>> In a given data frame I  want to compare character values of two columns.
>> My sample data looks like as follow,
>>
>> mydataframe <- read.table( text='ID  var1 var2
>>   R1   AA  AAA
>>   R2   AAA AAA
>>   R3    A  AAAA
>>   R4   AA   A
>>   R5   A  AAA', header = TRUE, as.is = TRUE )
>>
>> For each ID, I want  create the third column "dvar" as  difference
>> between var1 and var2
>>  Row1( R1)   the "dvar" value will be -1 and the complete  desired out
>> put looks like as follow.
>>
>>  ID    var1 var2   dvar
>>  R1   AA    AAA    -1
>>  R2  AAA  AAA      0
>>  R3    A    AAAA    -3
>>  R4   AA       A        1
>>  R5    A     AAA      -2
>>
>> How do i do this? Any help please?
>> Thank you
>>
>> ______________________________________________
>> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
>> 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.
>
> --
> Erin Hodgess, PhD
> mailto: [hidden email]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.
Reply | Threaded
Open this post in threaded view
|

Re: character comp

Erin Hodgess-2
In reply to this post by Erin Hodgess-2
Ok.  Try something like
nchar(mydataframe[,1])-nchar(mydataframe[,2])


On Sat, Feb 9, 2019 at 11:21 AM Erin Hodgess <[hidden email]>
wrote:

> Will it always be A’s or will there be a mix please?
>
> On Sat, Feb 9, 2019 at 11:06 AM Val <[hidden email]> wrote:
>
>> Hi  All,
>> In a given data frame I  want to compare character values of two columns.
>> My sample data looks like as follow,
>>
>> mydataframe <- read.table( text='ID  var1 var2
>>   R1   AA  AAA
>>   R2   AAA AAA
>>   R3    A  AAAA
>>   R4   AA   A
>>   R5   A  AAA', header = TRUE, as.is = TRUE )
>>
>> For each ID, I want  create the third column "dvar" as  difference
>> between var1 and var2
>>  Row1( R1)   the "dvar" value will be -1 and the complete  desired out
>> put looks like as follow.
>>
>>  ID    var1 var2   dvar
>>  R1   AA    AAA    -1
>>  R2  AAA  AAA      0
>>  R3    A    AAAA    -3
>>  R4   AA       A        1
>>  R5    A     AAA      -2
>>
>> How do i do this? Any help please?
>> Thank you
>>
>> ______________________________________________
>> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
>> 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.
>>
> --
> Erin Hodgess, PhD
> mailto: [hidden email]
>
--
Erin Hodgess, PhD
mailto: [hidden email]

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.
Reply | Threaded
Open this post in threaded view
|

Re: character comp

Rui Barradas
In reply to this post by Val-17
Hello,

The following will do it.

mydataframe$dvar <- c(sapply(mydataframe[-1], nchar) %*% c(1, -1))


Hope this helps,

Rui Barradas

Às 18:05 de 09/02/2019, Val escreveu:

> Hi  All,
> In a given data frame I  want to compare character values of two columns.
> My sample data looks like as follow,
>
> mydataframe <- read.table( text='ID  var1 var2
>    R1   AA  AAA
>    R2   AAA AAA
>    R3    A  AAAA
>    R4   AA   A
>    R5   A  AAA', header = TRUE, as.is = TRUE )
>
> For each ID, I want  create the third column "dvar" as  difference
> between var1 and var2
>   Row1( R1)   the "dvar" value will be -1 and the complete  desired out
> put looks like as follow.
>
>   ID    var1 var2   dvar
>   R1   AA    AAA    -1
>   R2  AAA  AAA      0
>   R3    A    AAAA    -3
>   R4   AA       A        1
>   R5    A     AAA      -2
>
> How do i do this? Any help please?
> Thank you
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> 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.
>

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.
Reply | Threaded
Open this post in threaded view
|

Re: character comp

Rui Barradas
In reply to this post by Erin Hodgess-2
Hello,

It should be

nchar(mydataframe[,2])-nchar(mydataframe[,3])


since the first column is ID.

Hope this helps,

Rui Barradas


Às 18:31 de 09/02/2019, Erin Hodgess escreveu:

> Ok.  Try something like
> nchar(mydataframe[,1])-nchar(mydataframe[,2])
>
>
> On Sat, Feb 9, 2019 at 11:21 AM Erin Hodgess <[hidden email]>
> wrote:
>
>> Will it always be A’s or will there be a mix please?
>>
>> On Sat, Feb 9, 2019 at 11:06 AM Val <[hidden email]> wrote:
>>
>>> Hi  All,
>>> In a given data frame I  want to compare character values of two columns.
>>> My sample data looks like as follow,
>>>
>>> mydataframe <- read.table( text='ID  var1 var2
>>>    R1   AA  AAA
>>>    R2   AAA AAA
>>>    R3    A  AAAA
>>>    R4   AA   A
>>>    R5   A  AAA', header = TRUE, as.is = TRUE )
>>>
>>> For each ID, I want  create the third column "dvar" as  difference
>>> between var1 and var2
>>>   Row1( R1)   the "dvar" value will be -1 and the complete  desired out
>>> put looks like as follow.
>>>
>>>   ID    var1 var2   dvar
>>>   R1   AA    AAA    -1
>>>   R2  AAA  AAA      0
>>>   R3    A    AAAA    -3
>>>   R4   AA       A        1
>>>   R5    A     AAA      -2
>>>
>>> How do i do this? Any help please?
>>> Thank you
>>>
>>> ______________________________________________
>>> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
>>> 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.
>>>
>> --
>> Erin Hodgess, PhD
>> mailto: [hidden email]
>>

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.
Reply | Threaded
Open this post in threaded view
|

Re: character comp

Erin Hodgess-2
In reply to this post by Rui Barradas
Nice, Rui!  Thanks

On Sat, Feb 9, 2019 at 11:55 AM Rui Barradas <[hidden email]> wrote:

> Hello,
>
> The following will do it.
>
> mydataframe$dvar <- c(sapply(mydataframe[-1], nchar) %*% c(1, -1))
>
>
> Hope this helps,
>
> Rui Barradas
>
> Às 18:05 de 09/02/2019, Val escreveu:
> > Hi  All,
> > In a given data frame I  want to compare character values of two columns.
> > My sample data looks like as follow,
> >
> > mydataframe <- read.table( text='ID  var1 var2
> >    R1   AA  AAA
> >    R2   AAA AAA
> >    R3    A  AAAA
> >    R4   AA   A
> >    R5   A  AAA', header = TRUE, as.is = TRUE )
> >
> > For each ID, I want  create the third column "dvar" as  difference
> > between var1 and var2
> >   Row1( R1)   the "dvar" value will be -1 and the complete  desired out
> > put looks like as follow.
> >
> >   ID    var1 var2   dvar
> >   R1   AA    AAA    -1
> >   R2  AAA  AAA      0
> >   R3    A    AAAA    -3
> >   R4   AA       A        1
> >   R5    A     AAA      -2
> >
> > How do i do this? Any help please?
> > Thank you
> >
> > ______________________________________________
> > [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> > 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.
> >
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> 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.
>
--
Erin Hodgess, PhD
mailto: [hidden email]

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.
Reply | Threaded
Open this post in threaded view
|

Re: character comp

Rui Barradas
After correcting the typo I tested both and yours is over twice as fast
as mine.

Rui Barradas

Às 19:08 de 09/02/2019, Erin Hodgess escreveu:

> Nice, Rui!  Thanks
>
> On Sat, Feb 9, 2019 at 11:55 AM Rui Barradas <[hidden email]
> <mailto:[hidden email]>> wrote:
>
>     Hello,
>
>     The following will do it.
>
>     mydataframe$dvar <- c(sapply(mydataframe[-1], nchar) %*% c(1, -1))
>
>
>     Hope this helps,
>
>     Rui Barradas
>
>     Às 18:05 de 09/02/2019, Val escreveu:
>      > Hi  All,
>      > In a given data frame I  want to compare character values of two
>     columns.
>      > My sample data looks like as follow,
>      >
>      > mydataframe <- read.table( text='ID  var1 var2
>      >    R1   AA  AAA
>      >    R2   AAA AAA
>      >    R3    A  AAAA
>      >    R4   AA   A
>      >    R5   A  AAA', header = TRUE, as.is <http://as.is> = TRUE )
>      >
>      > For each ID, I want  create the third column "dvar" as  difference
>      > between var1 and var2
>      >   Row1( R1)   the "dvar" value will be -1 and the complete
>     desired out
>      > put looks like as follow.
>      >
>      >   ID    var1 var2   dvar
>      >   R1   AA    AAA    -1
>      >   R2  AAA  AAA      0
>      >   R3    A    AAAA    -3
>      >   R4   AA       A        1
>      >   R5    A     AAA      -2
>      >
>      > How do i do this? Any help please?
>      > Thank you
>      >
>      > ______________________________________________
>      > [hidden email] <mailto:[hidden email]> mailing list
>     -- To UNSUBSCRIBE and more, see
>      > 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.
>      >
>
>     ______________________________________________
>     [hidden email] <mailto:[hidden email]> mailing list --
>     To UNSUBSCRIBE and more, see
>     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.
>
> --
> Erin Hodgess, PhD
> mailto: [hidden email] <mailto:[hidden email]>

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.
Reply | Threaded
Open this post in threaded view
|

Re: character comp

Val-17
In reply to this post by Erin Hodgess-2
Thank you Erin and Rui!


On Sat, Feb 9, 2019 at 1:08 PM Erin Hodgess <[hidden email]> wrote:

>
> Nice, Rui!  Thanks
>
> On Sat, Feb 9, 2019 at 11:55 AM Rui Barradas <[hidden email]> wrote:
>>
>> Hello,
>>
>> The following will do it.
>>
>> mydataframe$dvar <- c(sapply(mydataframe[-1], nchar) %*% c(1, -1))
>>
>>
>> Hope this helps,
>>
>> Rui Barradas
>>
>> Às 18:05 de 09/02/2019, Val escreveu:
>> > Hi  All,
>> > In a given data frame I  want to compare character values of two columns.
>> > My sample data looks like as follow,
>> >
>> > mydataframe <- read.table( text='ID  var1 var2
>> >    R1   AA  AAA
>> >    R2   AAA AAA
>> >    R3    A  AAAA
>> >    R4   AA   A
>> >    R5   A  AAA', header = TRUE, as.is = TRUE )
>> >
>> > For each ID, I want  create the third column "dvar" as  difference
>> > between var1 and var2
>> >   Row1( R1)   the "dvar" value will be -1 and the complete  desired out
>> > put looks like as follow.
>> >
>> >   ID    var1 var2   dvar
>> >   R1   AA    AAA    -1
>> >   R2  AAA  AAA      0
>> >   R3    A    AAAA    -3
>> >   R4   AA       A        1
>> >   R5    A     AAA      -2
>> >
>> > How do i do this? Any help please?
>> > Thank you
>> >
>> > ______________________________________________
>> > [hidden email] mailing list -- To UNSUBSCRIBE and more, see
>> > 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.
>> >
>>
>> ______________________________________________
>> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
>> 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.
>
> --
> Erin Hodgess, PhD
> mailto: [hidden email]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.