jcrosbie
2012-Nov-05 17:33 UTC
[R] custom function & missing value where TRUE/FALSE needed
I can't figure out why this function wont work. #Custom Function used fallInBand <- function(x){ #Assume the bands fall go up form 0 to 100 in intervals of 5 then from 100 to 1000 in intervals of 100. #returns the location (band number) if (is.na(x) == FALSE) { if(x < 100) {#bands of 5 if((x %% 5) >=0){#falls within the band Result = as.integer(x/5)+1 } else {#falls on the band Result = as.integer(x/5) }#end location within band if } else { # bands of 100 if((x %% 100) >0){#falls within the band Result = as.integer(x/100)+21 } else {#falls on the band Result = as.integer(x/100)+20 }#end location within band if }#end } else { Result = NA } return(Result) }#end function fallInBand #Data example: t = c(NA, 0, 10.44, 250, 30, 11.66, 20.18, 815, 629, 11.94, 10.4, 10.41, 20.11, 11.9, 10.45, 11.01, 141, 444, 389, 148.02, 98.88, 84.01, 388.02, 342, 339.06, 331, 188, 11.98, 10.95, 915, 14.65, 99.99, 67, 865, 333, 276, 11.93, 765, 166, 259, 246, 321, 19.98, 93, 186, 11.39, 928, 945, 869, 955, 612.11, 588, 944, 943, 678, 836, 804.1, 11.15, 848, 696.41, 10.37, 10.98, 10.9, 298.18, 180.97, 145.1, 895, 894, 30.14, 29.87, 21.12, 31.15, 31, 885, 115, 863, 884, 828, 189, 20.07, 194, 411.15, 887, 879, 411.75, 898, 823, 81.55, 875, 843, 131.68, 381, 281, 395, 105, 776, 240, 230.5, 80.25, 212.18, 198.3, 74.25, 374, 803, 45.22, 36.44, 10.5, 21.15, 10.78, 10.77, 38.17, 224, 72.4, 68, 399, 396.71, 42, 10.86, 11.99, 12.1, 11.88, 9.72, 10.72, 9.7, 8.47, 7.78, 7.4, 7.01, 7.02, 7.53, 10.85, 64.36, 888, 791, 735, 476, 10.75, 44, 895.1, 902, 584, 8.75, 838, 752, 747, 691, 688, 684.15, 679, 14, 265, 214, 148, 144.1, 136, 15.68, 348, 343, 112, 98, 855.35, 881, 891.6, 8.45, 900, 8.22, 897.15, 125.11, 8.8, 477, 648, 638, 719, 698, 480, 728.23, 779.31, 899.99, 9.35, 9.15, 9.3, 6.98, 904.35, 13.15, 13.1, 868.23, 810, 796, 746, 744, 13, 10.81, 10.62, 10.25, 10.21, 6.85, 6.8, 972.13, 11.13, 10.1, 10.15, 74.37, 954.33, 899.76, 299.55, 914.41) fallInBand(t) -- View this message in context: http://r.789695.n4.nabble.com/custom-function-missing-value-where-TRUE-FALSE-needed-tp4648500.html Sent from the R help mailing list archive at Nabble.com.
Sarah Goslee
2012-Nov-05 19:07 UTC
[R] custom function & missing value where TRUE/FALSE needed
In this line: if (is.na(x) == FALSE) { x is a vector, so you presumably want any(is.na(x)) or possibly all(is.na(x)) depending on what you are intending to check for. When I run your function (thanks for the reproducible example), the error I get is: Warning message: In if (is.na(x) == FALSE) { : the condition has length > 1 and only the first element will be used which is caused by that line. I don't get the error you report in the message subject. I think it's nicer to use if(!any(is.na(x))) { but both forms will work. It's definitely nicer to use <- for assignment. Sarah On Mon, Nov 5, 2012 at 12:33 PM, jcrosbie <james at crosb.ie> wrote:> I can't figure out why this function wont work. > > #Custom Function used > fallInBand <- function(x){ > #Assume the bands fall go up form 0 to 100 in intervals of 5 then from 100 > to 1000 in intervals of 100. > #returns the location (band number) > if (is.na(x) == FALSE) { > if(x < 100) {#bands of 5 > if((x %% 5) >=0){#falls within the band > Result = as.integer(x/5)+1 > } else {#falls on the band > Result = as.integer(x/5) > }#end location within band if > > > } else { # bands of 100 > if((x %% 100) >0){#falls within the band > Result = as.integer(x/100)+21 > } else {#falls on the band > Result = as.integer(x/100)+20 > }#end location within band if > }#end > } else { > Result = NA > } > > return(Result) > }#end function fallInBand > > #Data example: > t = c(NA, 0, 10.44, 250, 30, 11.66, 20.18, 815, 629, 11.94, 10.4, > 10.41, 20.11, 11.9, 10.45, 11.01, 141, 444, 389, 148.02, 98.88, > 84.01, 388.02, 342, 339.06, 331, 188, 11.98, 10.95, 915, 14.65, > 99.99, 67, 865, 333, 276, 11.93, 765, 166, 259, 246, 321, 19.98, > 93, 186, 11.39, 928, 945, 869, 955, 612.11, 588, 944, 943, 678, > 836, 804.1, 11.15, 848, 696.41, 10.37, 10.98, 10.9, 298.18, 180.97, > 145.1, 895, 894, 30.14, 29.87, 21.12, 31.15, 31, 885, 115, 863, > 884, 828, 189, 20.07, 194, 411.15, 887, 879, 411.75, 898, 823, > 81.55, 875, 843, 131.68, 381, 281, 395, 105, 776, 240, 230.5, > 80.25, 212.18, 198.3, 74.25, 374, 803, 45.22, 36.44, 10.5, 21.15, > 10.78, 10.77, 38.17, 224, 72.4, 68, 399, 396.71, 42, 10.86, 11.99, > 12.1, 11.88, 9.72, 10.72, 9.7, 8.47, 7.78, 7.4, 7.01, 7.02, 7.53, > 10.85, 64.36, 888, 791, 735, 476, 10.75, 44, 895.1, 902, 584, > 8.75, 838, 752, 747, 691, 688, 684.15, 679, 14, 265, 214, 148, > 144.1, 136, 15.68, 348, 343, 112, 98, 855.35, 881, 891.6, 8.45, > 900, 8.22, 897.15, 125.11, 8.8, 477, 648, 638, 719, 698, 480, > 728.23, 779.31, 899.99, 9.35, 9.15, 9.3, 6.98, 904.35, 13.15, > 13.1, 868.23, 810, 796, 746, 744, 13, 10.81, 10.62, 10.25, 10.21, > 6.85, 6.8, 972.13, 11.13, 10.1, 10.15, 74.37, 954.33, 899.76, > 299.55, 914.41) > > fallInBand(t) > > > > -- > View this message in context: http://r.789695.n4.nabble.com/custom-function-missing-value-where-TRUE-FALSE-needed-tp4648500.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.-- Sarah Goslee http://www.stringpage.com http://www.sarahgoslee.com http://www.functionaldiversity.org