IOANNA IOANNOU
2015-Oct-05 16:10 UTC
[R] Change values of a column based on the values of a third
Hello all,
I have a rather easy question. I want to add a column to the database which
will change the values of vector a based on the values to vector b. Any
ideas how?
For example:
Dat <- data.frame(a=
c('A','A','C','B','D','D','B'),
b=
c('N','N','Y','N','Y','N','N')
)
I want to add a column c which will change 'C' to 'D' if column
b is 'Y'.
> Dat
a b c
1 A N A
2 A N A
3 C Y D
4 B N B
5 C Y D
6 C N C
7 B N B
Any ideas?
Best,
ioanna
[[alternative HTML version deleted]]
Sarah Goslee
2015-Oct-05 16:40 UTC
[R] Change values of a column based on the values of a third
?ifelse
I changed Dat to a. have character type instead of factor, and b.
actually match what you show in your example instead of already having
D.
Dat <- data.frame(a=
c('A','A','C','B','C','C','B'),
b=
c('N','N','Y','N','Y','N','N'),
stringsAsFactors=FALSE )
Dat$c <- with(Dat, ifelse(b == "Y" & a == "C",
"D", a))
In general, I'd recommend you read one of the many excellent R
introductory guides out there.
On Mon, Oct 5, 2015 at 12:10 PM, IOANNA IOANNOU <ii54250 at msn.com>
wrote:> Hello all,
>
>
>
> I have a rather easy question. I want to add a column to the database which
> will change the values of vector a based on the values to vector b. Any
> ideas how?
>
>
>
> For example:
>
>
>
> Dat <- data.frame(a=
c('A','A','C','B','D','D','B'),
>
> b=
c('N','N','Y','N','Y','N','N')
)
>
>
>
> I want to add a column c which will change 'C' to 'D' if
column b is 'Y'.
>
>
>
>
>> Dat
>
> a b c
>
> 1 A N A
>
> 2 A N A
>
> 3 C Y D
>
> 4 B N B
>
> 5 C Y D
>
> 6 C N C
>
> 7 B N B
>
>
>
>
> Any ideas?
>
>
>
> Best,
>
> ioanna
>
--
Sarah Goslee
http://www.functionaldiversity.org
Jeff Newmiller
2015-Oct-05 16:42 UTC
[R] Change values of a column based on the values of a third
Either
Dat$c <- ifelse( "Y"==Dat$b, rep( "D", nrow(Dat) ), Dat$a
)
Or (more efficiently)
Dat$c <- Dat$a
Dat$c[ "Y"==Dat$b ] <- "D"
Also, beware of creating data frames without using the stringsAsFactors=FALSE
option if you plan to replace levels like this.
---------------------------------------------------------------------------
Jeff Newmiller The ..... ..... Go Live...
DCN:<jdnewmil at dcn.davis.ca.us> Basics: ##.#. ##.#. Live
Go...
Live: OO#.. Dead: OO#.. Playing
Research Engineer (Solar/Batteries O.O#. #.O#. with
/Software/Embedded Controllers) .OO#. .OO#. rocks...1k
---------------------------------------------------------------------------
Sent from my phone. Please excuse my brevity.
On October 5, 2015 9:10:29 AM PDT, IOANNA IOANNOU <ii54250 at msn.com>
wrote:>Hello all,
>
>
>
>I have a rather easy question. I want to add a column to the database
>which
>will change the values of vector a based on the values to vector b. Any
>ideas how?
>
>
>
>For example:
>
>
>
>Dat <- data.frame(a=
c('A','A','C','B','D','D','B'),
>
> b=
c('N','N','Y','N','Y','N','N')
)
>
>
>
>I want to add a column c which will change 'C' to 'D' if
column b is
>'Y'.
>
>
>
>
>> Dat
>
> a b c
>
>1 A N A
>
>2 A N A
>
>3 C Y D
>
>4 B N B
>
>5 C Y D
>
>6 C N C
>
>7 B N B
>
>
>
>
>Any ideas?
>
>
>
>Best,
>
>ioanna
>
>
>
>
> [[alternative HTML version deleted]]
>
>______________________________________________
>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.