Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create new column based on 4 values in another column

Tags:

r

if-statement

I want to create a new column based on 4 values in another column.

if col1=1 then col2= G; if col1=2 then col2=H; if col1=3 then col2=J; if col1=4 then col2=K. 

HOW DO I DO THIS IN R? Please I need someone to help address this. I have tried if/else and ifelse but none seems to be working. Thanks

like image 546
nolyugo Avatar asked Oct 05 '11 07:10

nolyugo


People also ask

How do you create a new column in a DataFrame based on another column?

Using apply() method If you need to apply a method over an existing column in order to compute some values that will eventually be added as a new column in the existing DataFrame, then pandas. DataFrame. apply() method should do the trick.

How do I get the value of a column in a DataFrame based on another column?

You can extract a column of pandas DataFrame based on another value by using the DataFrame. query() method. The query() is used to query the columns of a DataFrame with a boolean expression.


2 Answers

You could use nested ifelse:

col2 <- ifelse(col1==1, "G",         ifelse(col1==2, "H",         ifelse(col1==3, "J",         ifelse(col1==4, "K",                         NA  )))) # all other values map to NA 

In this simple case it's overkill, but for more complicated ones...

like image 95
Marek Avatar answered Sep 20 '22 22:09

Marek


You have a special case of looking up values where the index are integer numbers 1:4. This means you can use vector indexing to solve your problem in one easy step.

First, create some sample data:

set.seed(1) dat <- data.frame(col1 = sample(1:4, 10, replace = TRUE)) 

Next, define the lookup values, and use [ subsetting to find the desired results:

values <- c("G", "H", "J", "K") dat$col2 <- values[dat$col1] 

The results:

dat    col1 col2 1     2    H 2     2    H 3     3    J 4     4    K 5     1    G 6     4    K 7     4    K 8     3    J 9     3    J 10    1    G 

More generally, you can use [ subsetting combined with match to solve this kind of problem:

index <- c(1, 2, 3, 4) values <- c("G", "H", "J", "K") dat$col2 <- values[match(dat$col1, index)] dat    col1 col2 1     2    H 2     2    H 3     3    J 4     4    K 5     1    G 6     4    K 7     4    K 8     3    J 9     3    J 10    1    G 
like image 35
Andrie Avatar answered Sep 22 '22 22:09

Andrie