Alexander
Alexander

Reputation: 1005

How to use an if then statement in R to create a new variable based on another

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"]

Upvotes: 0

Views: 391

Answers (2)

rmuc8
rmuc8

Reputation: 2989

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.

Upvotes: 1

Se&#241;or O
Se&#241;or O

Reputation: 17432

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

Upvotes: 2

Related Questions