[R] ifelse(is.na), with function inside

Grześ gregorio99 at gmail.com
Fri Jun 19 22:48:14 CEST 2009



:) Thank you very much! I have already found solution. I have always problem
with connect logical value and vector :/  You know... everybody helped me a
little. Thanks very much again!

Grzesiek


Petr Pikal wrote:
> 
> Hi
> 
> r-help-bounces at r-project.org napsal dne 17.06.2009 14:59:35:
> 
>> 
>> This is my function:
>> 
>> zywnoscCalosc <- function( zywnosc, sklepik, sklslodycze) { 
>> 
>> b=as.vector(sklslodycze)
>> 
>> fun=function(a,b){
>> a=2+b
>> }
>> zywnosc=ifelse(is.na(sklepik),NA,fun(a,b))        #Here I have a problem 
> !
>> }
>> 
>> I always get: Error in 2 + b : non-numeric argument to binary operator
>> I think problem is in my vector b. What I should do to get restult my
>> function fun(a,b) inside "zywnosc"? 
> 
> It is REALLY difficult to understand what you want to achieve.
> 
> There is no problem in your function (I do not get any error when I copy 
> it to R) but it is a mystery how you would like to use it, there is no 
> output from the function.
> 
> I am also almost sure that a or b is not defined for function 
> "zywnoscCalosc". (Try to search "nested function" in help archive.
> 
> A wild guess is that
> 
> zywnosc=ifelse(is.na(sklepik),NA,sklslodycze+2)
> 
> is what you want, however without working example and/or explanation what 
> you REALLY want it is just a guess.
> 
> Regards
> Petr
> 
> 
> 
>> 
>> 
>> 
>> 
>> 
>> 
>> joscani wrote:
>> > 
>> > Try:
>> > 
>> > b<- ifelse(is.na(a),1,2)
>> > 
>> > 
>> > Grześ wrote:
>> >> 
>> >> 2 - is.na(a)  - it's superb! but I need call a function:  wy[i]<-
>> >> ifelse(((is.na(a))), call_fun1(x), call_fun2(x)
>> >> 
>> >> 
>> >> 
>> >> Gabor Grothendieck wrote:
>> >>> 
>> >>> Try:
>> >>> 
>> >>> 2 - is.na(a)
>> >>> 
>> >>> 
>> >>> On Tue, Jun 16, 2009 at 5:46 PM, Grześ<gregorio99 at gmail.com> wrote:
>> >>>>
>> >>>> Hi,
>> >>>> I have a vector a=c(NA, 3, 4, 4, NA, NA, 3) and I would like to use
>> >>>> is.na(a)
>> >>>> function to get a vector like this:
>> >>>> wy=(1,2,2,2,1,1,2) - you know, this vector create 1 or 2 depends on
>> >>>> value in
>> >>>> vector "a"
>> >>>>
>> >>>> This is my short code but something is wrong and I don't know 
> what...
>> >>>>
>> >>>> for (i in 1:7){
>> >>>>  a=c( NA, 3, 4, 4, NA, NA, 3)
>> >>>>  fun1=function(x){
>> >>>>  x=1
>> >>>>  print(x)
>> >>>>  }
>> >>>>  fun2=function(x){
>> >>>>  x=2
>> >>>>  print(x)
>> >>>>  }
>> >>>>  wy[i]<- ifelse(((is.na(a))), fun1(x), fun2(x))
>> >>>>  }
>> >>>> [1] 1
>> >>>> [1] 2
>> >>>> [1] 1
>> >>>> [1] 2
>> >>>> [1] 1
>> >>>> [1] 2
>> >>>> [1] 1
>> >>>> [1] 2
>> >>>> [1] 1
>> >>>> [1] 2
>> >>>> [1] 1
>> >>>> [1] 2
>> >>>> [1] 1
>> >>>> [1] 2
>> >>>> and I get a lots of warnings:
>> >>>> Warning messages:
>> >>>> 1: In wy[i] <- ifelse(((is.na(a))), fun1(x), fun2(x)) :
>> >>>>  number of items to replace is not a multiple of replacement length
>> >>>> 2: In wy[i] <- ifelse(((is.na(a))), fun1(x), fun2(x)) :
>> >>>>  number of items to replace is not a multiple of replacement 
> length...
>> >>>>
>> >>>> and as an result I get a vector only with 1
>> >>>>> wy
>> >>>> [1] 1 1 1 1 1 1 1
>> >>>>
>> >>>> I'll be very happy if someone help me
>> >>>>
>> >>>> --
>> >>>> View this message in context:
>> >>>> http://www.nabble.com/ifelse%28is.na%29%2C-with-function-inside-
>> tp24063455p24063455.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.
>> >>>>
>> >>> 
>> >>> ______________________________________________
>> >>> 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.
>> >>> 
>> >>> 
>> >> 
>> >> 
>> > 
>> > 
>> 
>> -- 
>> View this message in context: 
> http://www.nabble.com/ifelse%28is.na%29%2C-with-
>> function-inside-tp24063455p24073158.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.
> 
> ______________________________________________
> 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.
> 
> 

-- 
View this message in context: http://www.nabble.com/ifelse%28is.na%29%2C-with-function-inside-tp24063455p24118560.html
Sent from the R help mailing list archive at Nabble.com.




More information about the R-help mailing list