Hello! I wont to use a function is.na() I have two vectors:> a=c(1,NA,3,3,3) > b=c(0,0,0,0,0)and when I use is.na function it's ok:> is.na(a)[1] FALSE TRUE FALSE FALSE FALSE but I would create sth like this: for i in 1:length(a){ if (wsp[i] == is.na(a)) {b=43} } or like this if(is.na(a)) b=3 else a [1] 1 NA 3 3 3 but I always get an error: the condition has length > 1 and only the first element will be used Could you help me how I may avoid this problem and use function is.na inside function if - else Please -- View this message in context: http://www.nabble.com/if-%2B-is.na-tp24025136p24025136.html Sent from the R help mailing list archive at Nabble.com.
Grze? wrote:> > Hello! > I wont to use a function is.na() > > I have two vectors: >> a=c(1,NA,3,3,3) >> b=c(0,0,0,0,0) > and when I use is.na function it's ok: >> is.na(a) > [1] FALSE TRUE FALSE FALSE FALSE > > but I would create sth like this: > > for i in 1:length(a){ > if (wsp[i] == is.na(a)) {b=43} > } > or like this > > if(is.na(a)) b=3 else a > [1] 1 NA 3 3 3 > > but I always get an error: > the condition has length > 1 and only the first element will be used > > Could you help me how I may avoid this problem and use function is.na > inside function if - else > >Use "ifelse" instead of "if" -- View this message in context: http://www.nabble.com/if-%2B-is.na-tp24025136p24025414.html Sent from the R help mailing list archive at Nabble.com.
On 15/06/2009, at 7:48 AM, Grze? wrote:> > Hello! > I wont to use a function is.na() > > I have two vectors: >> a=c(1,NA,3,3,3) >> b=c(0,0,0,0,0) > and when I use is.na function it's ok: >> is.na(a) > [1] FALSE TRUE FALSE FALSE FALSE > > but I would create sth like this: > > for i in 1:length(a){ > if (wsp[i] == is.na(a)) {b=43} > } > or like this > > if(is.na(a)) b=3 else a > [1] 1 NA 3 3 3 > > but I always get an error: > the condition has length > 1 and only the first element will be used > > Could you help me how I may avoid this problem and use function > is.na inside > function if - else > Please?ifelse ###################################################################### Attention: This e-mail message is privileged and confidential. If you are not the intended recipient please delete the message and notify the sender. Any views or opinions presented are solely those of the author. This e-mail has been scanned and cleared by MailMarshal www.marshalsoftware.com ######################################################################
Your error message is because "if" wants a single value and you are giving it a vector. Typically you want to use functions "all" or any" to correct this error message (look them up ?all ?any) and eg if(any(is.na(...))) But in this case to accomplish the task you're after I don't even think you want to use an if. I am not going to give you precise code because I wasn't able to decipher exactly what you were trying to do but something like: b[is.na(a)] = 43 might be helpful. This line would put a 43 in b in the corresponding entry that was na in a. Good luck!.> Date: Sun, 14 Jun 2009 12:48:58 -0700 > From: gregorio99@gmail.com > To: r-help@r-project.org > Subject: [R] if + is.na > > > Hello! > I wont to use a function is.na() > > I have two vectors: > > a=c(1,NA,3,3,3) > > b=c(0,0,0,0,0) > and when I use is.na function it's ok: > > is.na(a) > [1] FALSE TRUE FALSE FALSE FALSE > > but I would create sth like this: > > for i in 1:length(a){ > if (wsp[i] == is.na(a)) {b=43} > } > or like this > > if(is.na(a)) b=3 else a > [1] 1 NA 3 3 3 > > but I always get an error: > the condition has length > 1 and only the first element will be used > > Could you help me how I may avoid this problem and use function is.na inside > function if - else > Please > > > -- > View this message in context: http://www.nabble.com/if-%2B-is.na-tp24025136p24025136.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help@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._________________________________________________________________ Lauren found her dream laptop. Find the PC that’s right for you. [[alternative HTML version deleted]]