Hello, I have a simple Data table like below:> DataV1 V2 1 1 3 2 5 4 And I like to apply a switch to all the values of the table. Exp for one value: switch(as.character(Data[2,1]), "1"="One", "2"="Two", "3"="Three", "4"="Four", "5"="Five" ) What would be the best way to do this? Thank you. Pooya THIS E-MAIL IS FOR THE SOLE USE OF THE INTENDED RECIPIENT(S) AND MAY CONTAIN CONFIDENTIAL AND PRIVILEGED INFORMATION.ANY UNAUTHORIZED REVIEW, USE, DISCLOSURE OR DISTRIBUTION IS PROHIBITED. IF YOU ARE NOT THE INTENDED RECIPIENT, PLEASE CONTACT THE SENDER BY REPLY E-MAIL AND DESTROY ALL COPIES OF THE ORIGINAL E-MAIL. [[alternative HTML version deleted]]
That would depend on the actual type of object that your Data variable is. Use
the dput function so we can get your example days in a reproducible way [1].
[1]
http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example
---------------------------------------------------------------------------
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 June 17, 2014 1:30:57 PM PDT, Pooya Lalehzari <plalehzari at
platinumlp.com> wrote:>Hello,
>
>I have a simple Data table like below:
>> Data
> V1 V2
>1 1 3
>2 5 4
>
>And I like to apply a switch to all the values of the table.
>Exp for one value:
>switch(as.character(Data[2,1]),
> "1"="One",
> "2"="Two",
> "3"="Three",
> "4"="Four",
> "5"="Five"
>)
>
>What would be the best way to do this?
>
>Thank you.
>
>Pooya
>
>
>
>
>
>THIS E-MAIL IS FOR THE SOLE USE OF THE INTENDED RECIPIENT(S) AND MAY
>CONTAIN
>CONFIDENTIAL AND PRIVILEGED INFORMATION.ANY UNAUTHORIZED REVIEW, USE,
>DISCLOSURE
>OR DISTRIBUTION IS PROHIBITED. IF YOU ARE NOT THE INTENDED RECIPIENT,
>PLEASE
>CONTACT THE SENDER BY REPLY E-MAIL AND DESTROY ALL COPIES OF THE
>ORIGINAL E-MAIL.
> [[alternative HTML version deleted]]
>
>______________________________________________
>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.
If you are not determined to apply switch(), you can apply apply():
Data <- data.frame(V1=c(1, 5), V2=c(3, 4))
Data
## V1 V2
## 1 1 3
## 2 5 4
label <- data.frame(freq=c(1:5), lbl=c("One", "Two",
"Three", "Four", "Five"))
label
## freq lbl
## 1 1 One
## 2 2 Two
## 3 3 Three
## 4 4 Four
## 5 5 Five
Data2 <- as.data.frame(apply(Data, 2, function(x) label[x, 2]))
Data2
## V1 V2
## [1,] "One" "Three"
## [2,] "Five" "Four"
This will work if Data is a data.frame, a matrix, or a table.
David Carlson
-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org]
On Behalf Of Pooya Lalehzari
Sent: Tuesday, June 17, 2014 3:31 PM
To: R help
Subject: [R] apply switch to a table
Hello,
I have a simple Data table like below:> Data
V1 V2
1 1 3
2 5 4
And I like to apply a switch to all the values of the table.
Exp for one value:
switch(as.character(Data[2,1]),
"1"="One",
"2"="Two",
"3"="Three",
"4"="Four",
"5"="Five"
)
What would be the best way to do this?
Thank you.
Pooya
THIS E-MAIL IS FOR THE SOLE USE OF THE INTENDED RECIPIENT(S) AND MAY CONTAIN
CONFIDENTIAL AND PRIVILEGED INFORMATION.ANY UNAUTHORIZED REVIEW, USE, DISCLOSURE
OR DISTRIBUTION IS PROHIBITED. IF YOU ARE NOT THE INTENDED RECIPIENT, PLEASE
CONTACT THE SENDER BY REPLY E-MAIL AND DESTROY ALL COPIES OF THE ORIGINAL
E-MAIL.
[[alternative HTML version deleted]]
______________________________________________
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.