R function to assign value based on multiple columns -


I have a function that provides values ​​(cat, dog or hare) in column C of a data frame based on the entry Does the column B:

  mydf  2 & amp; x <6) y  6) y <- "Rabbit" returns (y)} mydf $ c <- sapply (mydf $ b, myfunction)  

I would like to write a function that makes the assignment conditional, for example, the value of columns A and B can be in situations: A: the cat has been assigned if only and if mydf $ a == 1 & Mydf $ b == 1; B: Whenever mydf $ a = $ 2 mydf $ b, regardless of value, and when mydf $ a == 3 & Mydf $ b == 5; c: Other cases There is a problem with the syntax, no examples were found from the previous post.

you ifelse

  index end lie- as.character (interaction (mindf, sep =" ")) mydf $ c <- Ifelse (index == '11', 'cat', ifelse (substr (index, 1,1) == '2' | Indx == '35', 'dog', 'rabbit')) mydf # abc # 1 1 1 cat # 2 2 3 dogs # 3 3 5 dogs # 4 4 9 rabbit  

Comments

Popular posts from this blog

java - Can't add JTree to JPanel of a JInternalFrame -

javascript - data.match(var) not working it seems -

javascript - How can I pause a jQuery .each() loop, while waiting for user input? -