Hiro
2010-Apr-23 15:43 UTC
[R] Check character string for value and assign corresponding value in a new variable
Hi all,
Here is what I am trying to do. It doesn't seem like a complex command but I
am a beginner at R.
Lets say I have this variable:
test$v1 = rep(c("ACAM2343", "ACAM3838",
"ACPP2598", "ACPA8355", "DEAM234",
"DEPA38485"), each = 10)
1. if test$v1 character string contains "PP" or "PA" set
test$v2 value to
"WIN".
Alternatively,
2. if test$v1 character 3 and 4 equal "A" and M", set test$v2 to
"LOSE".
Please help!
Hiro
--
View this message in context:
http://r.789695.n4.nabble.com/Check-character-string-for-value-and-assign-corresponding-value-in-a-new-variable-tp2062391p2062391.html
Sent from the R help mailing list archive at Nabble.com.
Jorge Ivan Velez
2010-Apr-23 19:19 UTC
[R] Check character string for value and assign corresponding value in a new variable
Hi Hiro,
Here is a suggestion:
y <- rep(c("ACAM2343", "ACAM3838", "ACPP2598",
"ACPA8355", "DEAM234",
"DEPA38485"), each = 10)
y
x <- y
x[grepl("PA|PP", x)] <- "WIN"
x[substr(y, 3, 4) == 'AM'] <- "LOSE"
x
Another way would be using regular expressions directly. See ?regex for more
information.
HTH,
Jorge
On Fri, Apr 23, 2010 at 11:43 AM, Hiro <hpk2106@columbia.edu> wrote:
>
> Hi all,
>
> Here is what I am trying to do. It doesn't seem like a complex command
but
> I
> am a beginner at R.
>
> Lets say I have this variable:
> test$v1 = rep(c("ACAM2343", "ACAM3838",
"ACPP2598", "ACPA8355", "DEAM234",
> "DEPA38485"), each = 10)
>
> 1. if test$v1 character string contains "PP" or "PA"
set test$v2 value to
> "WIN".
>
> Alternatively,
>
> 2. if test$v1 character 3 and 4 equal "A" and M", set
test$v2 to "LOSE".
>
> Please help!
>
> Hiro
> --
> View this message in context:
>
http://r.789695.n4.nabble.com/Check-character-string-for-value-and-assign-corresponding-value-in-a-new-variable-tp2062391p2062391.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help@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.
>
[[alternative HTML version deleted]]