0

I have two datasets: mydata2 and mydata3

Mydata2: 2 columns in it: Destination.DisplayName and Partner.Type

(Partner.Type is based off Destination.DisplayName)

Mydata3: 2 columns in it: If and Then

I need a line of code that tests if Destination.DisplayName is equal to If, and if it is, set partner.type to the value in Then

Right now this is what I have

     mydata2$Partner.Type[  mydata2$Destination.DisplayName %in% mydata3$If] = as.character((mydata3$Then[match(mydata2$Destination.DisplayName, mydata3$If)]))

Does anyone see what is wrong here?

Jon Pop
  • 33
  • 6
  • 2
    Welcome to SO. First of all you should read [here](http://stackoverflow.com/help/how-to-ask) about how to ask a good question; a good question has better changes to be solved and you to receive help. On the other hand a read of [this](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) is also good. It explains how to create a reproducible example in R. Help users to help you by providing a piece of your data a desired output and things you have tried so far. – SabDeM Sep 15 '15 at 20:08

1 Answers1

1

I suggest using ifelse function:

mydata2$Partner.Type <- ifelse(  mydata2$Destination.DisplayName == mydata3$If,
                                mydata3$Then,
                                mydata2$Partner.Type)

EDIT

If columns have not have the same lengths then try sapply

sapply(mydata2$Destination.DisplayName, function(element){
   if ( element %in% mydata3$If ){
   return(mydata3$Then[which(element == mydata3$If)])
} else {
return(mydata2$Partner.Type[which(element == mydata2$Destination.DisplayName)])
}
}) -> mydata2$Destination.DisplayName

EDIT2

Or you can use dplyr package and left_join function.

library(dplyr)
mydata2 %>%
   left_join(mydata3, 
              by = c("Destination.DisplayName" = "If")
   ) -> joined_mydatas

ifelse(is.na(joined_mydatas$Then),
        joined_mydatas$Partner.Type,
        joined_mydatas$Then) -> mydata2$Partner.Type
Marcin
  • 7,834
  • 8
  • 52
  • 99
  • I believe we are not allowed to use the " == " in the ifelse due to setting two columns equal to each other. I have tried the %in% command using this as well and it seems to appear as if a lot of the results are incorrect in the Partner.Type column – Jon Pop Sep 15 '15 at 20:53
  • 1
    You did not write they have different dimensions. I've edited my question. Try sapply? – Marcin Sep 15 '15 at 21:17
  • 1
    Destination.DisplayName had thousands of rows in it while the If column only has 20 entries. I tried using the sapply but I receive the error: "Error: unexpected ')' in: " return(mydata2$Partner.Type[which(element == mydata2$Destination.DisplayName)]) })"" – Jon Pop Sep 15 '15 at 21:51
  • 1
    Try now. There wasn't one parenthesis at the end. I can not compile code because you have not upload a sample data for this :P – Marcin Sep 15 '15 at 21:59
  • 1
    Check out my edit2 -> I think's is the most sql-like solution :) – Marcin Sep 15 '15 at 22:06
  • 1
    The second edit works extremely well, very creative solution as well. Can not thank you enough. -Jon – Jon Pop Sep 15 '15 at 22:20
  • 1
    Ok so you can just vote-up my answer and accept it as a solution :) – Marcin Sep 15 '15 at 22:24
  • 1
    I was trying to! Stack overflow wont let me upvote without 15-rep points.. – Jon Pop Sep 15 '15 at 22:41
  • 1
    And they even put your question as an off-topic :) – Marcin Sep 15 '15 at 22:43
  • 1
    Ok now you've got 16 :) – Marcin Sep 15 '15 at 22:45
  • I keep getting the error "Error: invalid subscript type 'integer'' when using your code in R shiny. Any thoughts? – Jon Pop Sep 16 '15 at 19:31
  • I suggest you create a new question with more elaborated code that each person can copy into their computer and reproduce your error. Such guesing is way to hard – Marcin Sep 17 '15 at 08:52