15

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.

4
  • All the answers below are the correct route. However, if you are using if/else then the else needs to be on the same line as the closing }` from your if statement. Commented Jan 7, 2013 at 18:53
  • for (i in 1 : nrow(dse)){ for (j in 3: ncol(dse)){ if ( dse[i,j] == 4230) { dse$dse1 = 1 } else{ dse$dse1 = 0 } if (dse[i,j] %in% (4567)) {dse$dse2 = 1} else{dse$dse2 = 0} } } I change the codes as above, there is no error, But the result is not correct. Why? THnaks Commented Jan 8, 2013 at 19:15
  • Not sure what to make of your comment. You can edit your question as needed. Also, if any of the answers below has solved the problem, please mark it as answered by clicking the green check mark by the question so we all know its been resolved. Commented Jan 8, 2013 at 19:18
  • I know the problems, the dse$dse1 should be dse$dse[i], and dse$dse2 should be dse$dse2[i]. Thanks for your help and answers Commented Jan 8, 2013 at 19:32

6 Answers 6

25

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)
 })
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks. The dse1 maybe has many diagnoses, this is, if dse[i,j] %in% ("2345","3456","5678") {dse1 =1 ). And the if codes have any woring? how to correct them
I am not sure what you want, please add more information to your question.
@user1582755 - To adjust the code for multiple matches I think you would just replace the corresponding lines above like: dse1 = ifelse(diag1 %in% c(2345,3456,5678), 1, 0)
dse1 = 1 if diag1 or diag2, etc in('224','334',333' etc) ; dse2 = 1 if diag1 or diag2 or diag3(if having) in('444','435',etc)
13

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

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

2 Comments

+1 nice answer, if feel however that using ifelse is a little more transparent. Also, you can have other vales than 0 and 1.
This is definitely the best answer. ifelse(cond, 1, 0) should never be used.
5

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.

1 Comment

Thanks for all answers. If we change the statements like these if diag1 OR diag2 contains 4230 then dse1 = 1 else dse1 = 0 if diag2 or diag1 contains 4567 then dse2 =1 else dse2 = 0 and there are many diags in which there are many codes, so there are many dses. What are the neat answers
4

You can use transform:

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

1 Comment

Thanks. The dse1 maybe has many diagnoses, this is, if dse[i,j] %in% ("2345","3456","5678") {dse1 =1 ).
0

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

Comments

0

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))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.