0

I have a data frame with Column1, which can take the value of any letter of the alphabet. I want to create a second column that spells out the number corresponding to that letter. I am trying to do this with an if then statement... But keep getting an error. Sorry this is a simple question but I have tried the R for dummies website http://www.dummies.com/how-to/content/how-to-use-if-statements-in-r.html with no luck!

x$Column2 <- NULL if (x$Column1 == "A") then[x$Column2 <- "One"]

Alexander
  • 977
  • 1
  • 13
  • 29
  • 1
    use accolades (`{` and `}`) instead of brackets (`[` and `]`), and make sure that you put line breaks. – Jealie Feb 10 '15 at 21:23

2 Answers2

2

The best way to do this is create a reference table:

>Reference = data.frame(Number = c("One", "Two", "Three", "Four"), Letter = c("A", "B", "C", "D"))
> Reference
  Number Letter
1    One      A
2    Two      B
3  Three      C
4   Four      D
> Data = data.frame(Letter = c("B", "B", "C", "A", "D"))
> Data
  Letter
1      B
2      B
3      C
4      A
5      D

Then you can find the indices:

> Indices = sapply(Data$Letter, function(x) which(x == Reference$Letter))
> Indices
[1] 2 2 3 1 4

And use them to create the column

> Data$Number = Reference[Indices,]$Number
> Data
  Letter Number
1      B    Two
2      B    Two
3      C  Three
4      A    One
5      D   Four
Señor O
  • 17,049
  • 2
  • 45
  • 47
  • After loading the `dplyr` package you could run `left_join(Data,Reference)` to accomplish the same thing. – Sam Firke Feb 10 '15 at 23:10
1

To my understanding, it is like creating a dummy variable, what you want to do here. Try

> x$dummy <- as.numeric(Column1 != "A")

and you should get 0 for all A's and 1 for other values.

Look at Generate a dummy-variable for further information.

Community
  • 1
  • 1
rmuc8
  • 2,869
  • 7
  • 27
  • 36