-2

I 've got an R assignment in which I have to add a column to my matrix. It's about dates(time zones), I use dplyr and lubridate libraries.

So I want from the below table to according to the state column to add its OlsonName(i.e. NSW -> Australia/NSW)

    Event.ID  Database     Date.Time      Nearest.town  State   *OlsonName*
1    20812     Wind     23/11/1975 07:00    SYDNEY       NSW  *Australia/NSW*
2    20813    Tornado   02/12/1975 14:00    BARHAM       NSW  *Australia/NSW*

I implement that with a function and a loop:

#function 
addOlsonNames <- function(aussieState,aussieTown){
 if(aussieState=="NSW"){
  if(aussieTown=="BROKEN HILL"){
    value <- "Australia/Broken_Hill";
  }else{
    value <- "Australia/NSW"
 }
 }else if(aussieState=="QLD"){
  value <- "Australia/Queensland"
 }else if(aussieState=="NT"){
  value <- "Australia/North"
 }else if(aussieState=="SA"){
  value <- "Australia/South"
 }else if(aussieState=="TAS"){
  value <- "Australia/Tasmania"
 }else if(aussieState=="VIC"){
  value <- "Australia/Victoria"
 }else if(aussieState=="WA"){
  value <- "Australia/West"
 }else if(aussieState=="ACT"){
  value <- "Australia/ACT"
 }
 else{
  value <- "NAN"
 }
 return(value)
}

#loop
for(i in 1:nrow(aussieStorms)){
 aussieStorms$OlsonName[i] <- addOlsonNames(State[i],Nearest.town[i])
}

Most of the instances are classified correctly like on my table above but some of the instances are misclassified(i.e. State~TAS -> OlsonName~Australia/West. Altough I have some State~TAS -> OlsonName~Australia/Tasmania).

Seems strange to me. What might be the issue ?


Update: I also tried mutate() and that's what I got:

 aus1 <- mutate(aussieStorms,OlsonXYZ = addOlsonNames(State,Nearest.town))

 Warning messages:
  1: In if (aussieState == "NSW") { :
  the condition has length > 1 and only the first element will be used
  2: In if (aussieTown == "BROKEN HILL") { :
  the condition has length > 1 and only the first element will be used
20-roso
  • 253
  • 1
  • 14
  • people if you downvote at least say the reason, If you cannot solve this question doesn't mean that you have to downvote. – 20-roso Oct 31 '15 at 13:31
  • 2
    If you get down votes you should look into how you can improve your question. One of the first stop should be the help pages. Don't assume that people downvote because they couldn't answer a question. That's pretty much never the case. The problem is usually that you don't provide sufficient information. Finally, please study [this FAQ](http://stackoverflow.com/q/5963269/1412059). – Roland Oct 31 '15 at 13:39
  • firstly thnx for the link, then leaving no comments though it doesn't let me know what is wrong, I double-checked my post, it might be a strange case but I am standing helplessly for any help. Read this [link](http://meta.stackexchange.com/questions/135/encouraging-people-to-explain-downvotes/31166#31166?newreg=f8af9681283148d8a1c7c50aa7c2e50d) so you can get my point of view. – 20-roso Oct 31 '15 at 13:46
  • The main problem I have with this question (I didn't downvote, I voted to close as unclear ...) is that we really need a *reproducible* example: http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example . Since there are no *obvious* bugs in your code, there's no way forward. – Ben Bolker Oct 31 '15 at 13:56
  • 1
    Do you need `aussieStorms$OlsonName[i] <- addOlsonNames(aussieStorms$State[i],aussieStorms$Nearest.town[i])`? – Ben Bolker Oct 31 '15 at 13:57
  • 2
    I actually believe that this could be solved with a simple merge/join operation, but can't be sure because we neither have representative and reproducible input nor corresponding expected output. – Roland Oct 31 '15 at 14:00
  • @BenBolker. Firstly thnx very much about your straightforward comment appreciate it, me either don't understand what's happening, unfortunately I cannot upload any pictures for a clearer view of what I am seeing. I will try as you suggest different ways like `join` as recommended and hope for the best. Thnx for the effort guys. – 20-roso Oct 31 '15 at 14:26
  • 1
    it's not about uploading pictures; the link that @Roland and I suggested goes through a lot of ways that you can help people answer by providing the information they need to reproduce your problem, e.g. by editing your question to include the output of an appropriate `dput()` call, or by posting your data somewhere and providing a link. – Ben Bolker Oct 31 '15 at 14:42
  • 1
    The problem with `mutate()` is that it expects a vectorized operation. Please post a reproducible example (!!) (Or check @Spacedman's answer) – Ben Bolker Oct 31 '15 at 14:44

1 Answers1

1

If Ben Bolker's comment is right then the problem is in here:

for(i in 1:nrow(aussieStorms)){
 aussieStorms$OlsonName[i] <- addOlsonNames(State[i],Nearest.town[i])
}

in that the values passed to addOlsonNames are not coming from rows of the aussieStorms data frame. If R isn't giving an error, then it must be getting State[i] from another object called State in your R workspace. Similarly for Nearest.town. If those objects aren't the same as the ones in your aussieStorms data frame, that would explain the apparent misclassification.

[Its also possible that you've used attach on a data frame at some point, and State is being got from that. But attaching data frames is a bad idea as you can see here...]

Ben's solution, ie making them aussieStorms$State and aussieStorms$Nearest.town look good to me.

Spacedman
  • 92,590
  • 12
  • 140
  • 224
  • That is embarrassing, I cleaned all my environment and after a couple of tries everything worked out well. Thnx all especially @Ben Bolker, I' ll be more considerate next time. – 20-roso Oct 31 '15 at 14:50
  • I think it might be worth asking another question about how to improve your function. If it was just a swap on the State value it would be trivially vectorisable (ie you could do a whole column without a loop), but the slight complication of NSW mapping to two possible Olson Names makes it slightly more interesting and possibly worthy of a new question. Plus it would give you another chance to write a well-formatted and properly reproducible question :) – Spacedman Oct 31 '15 at 15:50