user1582755
user1582755

Reputation: 235

add column values based on other columns in data frame using for and if

I have a dataframe like this:

     id        adit     diag1   diag2       
      2       3         4230    2234        
      3       5         3345    4456        
      4       6         4567    4467

I would like to add other 2 columns, dse1 and dse2 using the pseudo-code below:

if diag1 contains 4230 then dse1 = 1 else dse1 = 0

if diag2 contains 4567 then dse2 =1  else dse2 = 0

I used this:

for (i in 1 : nrow(dse)){
  for (j in 3: ncol(dse)){
     if dse[i,j] %in% ("4320"){dse$dse1 = 1}
        else{dse$dse1 = 0}
    if dse[i,j] %in% ("4567"){dse$dse2 = 1}
        else{dse$dse2 = 0} 
  }
}

But these do not work.

Upvotes: 15

Views: 73088

Answers (6)

pedrostrusso
pedrostrusso

Reputation: 388

A solution using tidyverse:

x = data.frame(id = c(2, 3, 4), 
               adit=c(3, 5, 6), 
               diag1=c(4230, 3345, 4567), 
               diag2=c(2234, 4456, 4467))

x %>% mutate(dse1 = if_else(diag1 == 4230, 1, 0), 
             dse2 = if_else(diag2 == 4567, 1, 0))

Upvotes: 0

IndPythCoder
IndPythCoder

Reputation: 753

you can also use:

ifelse():

dat <- data.frame(id = c(2,3,4), adit = c(3,5,6),diag1 = c(4230,3345,4567), diag2 =            c(2234,4567,4467))
dat$dse1 <- ifelse(dat$diag1 == 4230,1,0)
dat$dse2 <- ifelse(dat$diag2 == 4567,1,0)
dat

Upvotes: 0

Sven Hohenstein
Sven Hohenstein

Reputation: 81683

You can use transform:

transform(dse, dse1 = as.numeric(diag1 == 4230),
               dse2 = as.numeric(diag2 == 4567))

Upvotes: 4

Paul Hiemstra
Paul Hiemstra

Reputation: 60924

No need to use a loop, just use ifelse, for example

dse = within(dse, {
    dse1 = ifelse(diag1 == 4230, 1, 0)
    dse2 = ifelse(diag2 == 4567, 1, 0)
 })

Upvotes: 25

Theodore Lytras
Theodore Lytras

Reputation: 3965

Like this:

dse$dse1<-0
dse$dse2<-0
dse$dse1[dse$diag1==4230]<-1
dse$dse2[dse$diag2==4567]<-1

Please get yourself a good R tutorial (such as this) and read all about index vectors.

Upvotes: 5

Tyler Rinker
Tyler Rinker

Reputation: 109844

Don't use the if/else. Go vectorized as in:

dat$dse1 <- as.numeric(dat$diag1 == 4230)
dat$dse2 <- as.numeric(dat$diag2 == 4567)

Upvotes: 13

Related Questions