Message-ID: <116db21f-bd8f-66c6-e553-1756221cd033@sapo.pt>
Date: 2019-02-09T18:55:02Z
From: Rui Barradas
Subject: character comp
In-Reply-To: <CAJOiR6bkWri5o6ebm7D3cs9V2KbdoBwbdMJkzVA=a4A_J4PFHw@mail.gmail.com>
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
>
> ______________________________________________
> R-help at r-project.org 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.
>