0

I'm trying to use sapply instead of a 'for' loop but I'm not getting the result I'm expecting, I've tested each line separated and the code is working but when I use sapply is not. I'm looking for some hints on what might be wrong:

event <- c('Astronomical Low Tide', 'Avalanche', 'Blizzard', 'Coastal Flood', 
           'Cold/Wind Chill', 'Debris Flow', 'Dense Fog', 'Dense Smoke', 'Drought',
           'Dust Devil', 'Dust Storm','Excessive Heat', 'Extreme Cold/Wind Chill',
           'Flash Flood', 'Flood', 'Frost/Freeze', 'Funnel Cloud', 'Freezing Fog',
           'Hail', 'Heat', 'Heavy Rain', 'Heavy Snow', 'High Surf', 'High Wind',
           'Hurricane/Typhoon', 'Ice Storm', 'Lake/Effect Snow', 'Lakeshore Flood',
           'Lightning', 'Marine Hail', 'Marine High Wind', 'Marine Strong Wind',
           'Marine Thunderstorm Wind', 'Rip Current', 'Seiche', 'Sleet',
           'Storm Surge/Tide', 'Strong Wind', 'Thunderstorm Wind', 'Tornado',
           'Tropical Depression', 'Tropical Storm', 'Tsunami', 'Volcanic Ash',
           'Waterspout', 'Wildfire', 'Winter Storm', 'Winter Weather')

replace <- function(dt, x, col) {
  idx <- grep(paste('(?i)', event[x], sep = ''), dt[, col])
  dt[idx, col] <- event[x]
}

sapply(1:length(event), function(x) replace(stormdata, x, 8))

Basically, what I'm trying to do is to use every value on the event variable as a pattern on the grep function within the custom made replace function then I get the index of the rows that matched my pattern and stored them in the idx variable. After that I want to replace the rows in the data frame that correspond to the idx values with the value contained in the event variable.

I'm trying to create a loop with the sapply function to use every value on the event variable, so I want a loop that goes 48 times looking for each pattern in the data frame stormdata on its 8th column and replace them. BUT my code does nothing, after running it the data remains the same, no substitutions. When I run each line separately without the sapply it works.

I've looking everywhere, I can't find why isn't working. Help.

adolfohc
  • 235
  • 1
  • 2
  • 8
  • 1
    It would be more helpful if you also included a sample `stormdata` object to make your example [reproducible](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). In general, with functional programming languages you don't expect your functions to have side effects. If you want to change an object, your function should return a new, updated object rather than try to edit an existing one in place. When you edit a variable in a function, those changes are generally only visible inside that function and disappear when the function terminates. – MrFlick Feb 23 '15 at 03:50
  • The only way to see what the `sapply(.)` call does is to assign the result to a symbol. It's fairly dangerous at your early efforts in R to assign it back to`stormdata` but why not assign it to `stormtemp` and then look at it? – IRTFM Feb 23 '15 at 04:42

1 Answers1

0

Try using global assignment eg stormdata[idx, col] <<- event[x] in your function. Not clean but probably will work.

Neal Fultz
  • 9,282
  • 1
  • 39
  • 60