[R] ifelse(is.na), with function inside
Petr PIKAL
petr.pikal at precheza.cz
Wed Jun 17 15:22:54 CEST 2009
Hi
r-help-bounces at r-project.org napsal dne 17.06.2009 07:51:27:
>
> 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)
You did not grasp how to use ifelse. It goes not cycle through logical
vector is.na(a).
from help page
Arguments
test
an object which can be coerced to logical mode.
yes
return values for true elements of test.
no
return values for false elements of test.
Details
If yes or no are too short, their elements are recycled. yes will be
evaluated if and only if any element of test is true, and analogously for
no.
Missing values in test give missing values in the result.
Value
A vector of the same length and attributes (including class) as test and
data values from the values of yes or no. The mode of the answer will be
coerced from logical to accommodate first any values taken from yes and
then any values taken from no.
so for one call ifelse(a, b, c) you will get one vector of responses which
are b for TRUE values of a and c for false values of a.
You want to put this vector to one slot in wy (wy[i]) for which you will
get warning. If your call_fun(x) returns scalar you get as a result of
this
ifelse(((is.na(a))), call_fun1(x), call_fun2(x)
vector with length a.
You can store it in a list or data frame.
Regards
Petr
>
>
>
> 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-tp24063455p24067395.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.
More information about the R-help
mailing list