Displaying 20 results from an estimated 2320 matches for "ifelse".
2011 Sep 12
1
nested anova<-R chrashing
...t;clone"
(/plant genotype) on the survival of eggs (due to predation). Each plant
was provided with three different sizes of clutches (45,15,5) and had
pseudo-replications of size 15 and 5 on it.
_Question 1: right code for nested Anova_
Here part of the code I used:
clone1<-as.factor(ifelse(clone=="Gudrun",1,ifelse(clone=="Loden",2,ifelse(clone=="021",3,4))))
plant1<-as.factor(ifelse(plant==1,1,ifelse(plant==2,2,ifelse(plant==3,3,ifelse(plant==4,4,ifelse(plant==5,5,ifelse(plant==6,6,
ifelse(plant==7,7,ifelse(plant==8,8,ifelse(plant==9,9,ifelse(plant=...
2008 Nov 19
2
simplify this instruction
Hi R users,
Is there a way to simplify this instruction:
ifelse(B=="0","A",
ifelse(B=="1","A",
ifelse(B=="2","A",
ifelse(B=="3","A",
ifelse(B=="4","A",
ifelse(B=="5","A",
ifelse(B=="6","A",
ifelse(B=="7","A&q...
2008 Oct 20
5
Combining all possible values of variables into a new...
I'm trying to create a new column in my data.frame where subjects are categorized depending on values on four other columns. In any other case I would just nest a few ifelse statements, however, in this case i have 4*6*2*3=144 combinations and i get weird 'context overflow' errors. So I wonder if there is a more efficient way of doing this.
For illustrational purposes, let's say i have:
x<-c(1,0,0,1,0,0,1,0,0,1)
y<-c(1,3,2,3,2,1,2,3,2,3)
z<-c(1,...
2012 Aug 24
6
updating elements of a vector sequentially - is there a faster way?
...hat are 1 million or longer and need to repeat that process
several hundred times. The for loop works but is slow. If there is a faster
way, please let me know.
probs <- c(.1, .3, .2, .4, .7, .9, .3, .4, .5, .6)
p10 <- 0.6
p00 <- 0.4
vec1 <- rep(0, 10)
for (i in 2:10) {
vec1[i] <- ifelse(vec1[i-1] == 0,
ifelse(probs[i]<p10, 0, 1),
ifelse(probs[i]<p00, 0, 1))
}
Thanks
GG
[[alternative HTML version deleted]]
2009 Feb 10
7
ifelse()
I have a problem with ifelse(), I do not understand how it works.
> X<-c(2,2,1,1,0,0)
> str(X)
num [1:6] 2 2 1 1 0 0
> Y<-ifelse(X>0,1,0)
> Y
[1] 1 1 1 1 0 0
>
Can some one explain what is going on, I do not understand what ifelse is
doing in this case. Can someone explain the output Y.
Thanks
--...
2010 May 26
4
More efficient way to use ifelse()?
# This is more about trying to find a more effecient way to code some
simple vectorized computations using ifelse().
# Say you have some vector representing a factor with a number of
levels (6 in this case), representing the location that samples were
collected.
Population <- gl( n=6, k=5,length=120, labels =c("CO", "CN","Ga","KO",
"Mw", "Ng"))...
2010 Mar 10
1
ifelse logic and multiple assignments
...en logic along with a "do" statement to
make several things happen. While I could do the same thing in R
using a "for" loop, and "if" and {}, I've read that loops are less
common in R and I wonder if I'm doing things ineffectively.
Below I've used a "ifelse" function to make an assignment to the
variable FUT.direction, but what I'd really like to do is make several
assignments based on the same "ifelse" logic. Since the "ifelse"
wants to create a vector the same size as the logical condition it
doesn't seem obvious ho...
2005 Jun 27
2
simplifying the code
dear list:
I need help to achieve a simpler code to complete a
task I'm performing.
here is an example:
dat<-expand.grid(a=seq(1,5),b=seq(1000,1005))
I want to add a new column dat$c in that:
t1<-ifelse(dat$a==1&dat$b==1001,1001,0)
t2<-ifelse(dat$a==2&dat$b==1002,1001,0)
t3<-ifelse(dat$a==3&dat$b==1003,1001,0)
t4<-ifelse(dat$a==1&dat$b==1002,1002,0)
t5<-ifelse(dat$a==2&dat$b==1003,1002,0)
t6<-ifelse(dat$a==1&dat$b==1003,1003,0)
dat$c<-t1+t2+t3+t4+t5+t6
M...
2009 Feb 13
2
extracting parts of words or extraxting letter to use in ifelse-func.
Hello
I want to make some variables with the ifelse-function, but i don't know how
to do it.
I want to make these five variables;
b2$PRRSvac <- ifelse(b2$status=='A' | b2$status=='Aa',1,0)
b2$PRRSdk <- ifelse(b2$status=='B' | b2$status=='Bb',1,0)
b2$sanVac <- ifelse(b2$status=='C' | b2$statu...
2007 Apr 17
3
Is this a bug?
I have found a strange "ifelse" behaviour (I think)
This works:
> ifelse(T,1+1,1+2)
[1] 2
> ifelse(F,1+1,1+2)
[1] 3
Maybe I missed something about R internals, but why
> ifelse(T,print("hello"),print("goodbye"))
[1] "hello"
[1] "hello"
> ifelse(F,print("hello"...
2009 Oct 24
2
ifelse
When I run this code from an R-script:
ddd = 360 + round ( atan2(-u,-v) / d2r )
print(class(ddd))
print(ddd)
ifelse ( ddd>360, ddd-360, ddd )
print(ddd)
I get this output:
[1] "numeric"
[1] 461 213 238 249 251
[1] 461 213 238 249 251
Why does ifelse not change the 461 to 101?
I recreated the vector ddd and ran the same ifelse
code and it did work as expected.
> ddd <...
2005 Jun 22
2
Trouble with ifelse and if statement (PR#7962)
...#39; and 'h' are both numeric vectors)
> i <- abs(g-h)
creating a vector 'i' with values,
> i
[1] 0.08 0.00 0.33 0.00 0.00 0.00 0.00 0.33 0.00 0.00 0.08 0.08 0.20 0.00 0.13
Now, I want to create a new vector =1 whenever 'i' = 0.33 and =0 otherwise. I
use the 'ifelse' function to do this but when I do, I find
> ifelse(i==0.33,1,0)
[1] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
even though the third and eighth elements of 'i' clearly equal 0.33. Unless I
have missed something, 'ifelse' should return a vector that looks like,
[1] 0 0 1 0 0 0 0 1 0 0...
2010 Feb 23
3
Use 2 "ifelse" to sort data
Dear R users,
I have a question how to use 2 "ifelse" to sort my data. Such as
from 11 to 20 assign to A; 6 to 10 assign to B, and the rest of them assign
to C
a<-1:20
tt<-ifelse(a>10, "A",no=ifelse( 5< a <=10, "B", "C"))
Many Thanks
Chunhao
--
View this message in context: http://n4.nabble.com...
2010 Aug 18
5
ifelse command
...)
if(x<=1/10){y[i]=1}
else if(x<=2/10){y[i]=2}
else if(x<=4/10){y[i]=3}
else if(x<=7/10){y[i]=4}
else if(x<=9/10){y[i]=5}
else{y[i]=6}
}
bar<-barplot(table(y))
table<-table(y)
return(list(bar,table))
}
mydie2()
but I also want to try to create the same dice stimulation using ifelse()
statement, but all hell break loose when I attempt to use the command below
(the barplot only shows the results to be 6), can anyone tell me what went
wrong please:
mydie2<-function(n=1000,y=NULL,...){
for(i in 1:n){
x<-sample(1:6,n,replace=TRUE,prob=c(1,1,2,3,2,1)/10)
x=runif(n)
ifelse((x&...
2018 May 03
1
Proposed speedup of ifelse
I propose a patch to ifelse that leverages anyNA(test) to achieve an
improvement in performance. For a test vector of length 10, the change
nearly halves the time taken and for a test of length 1 million, there
is a tenfold increase in speed. Even for small vectors, the
distributions of timings between the old and the propose...
2016 Nov 27
1
ifelse() woes ... can we agree on a ifelse2() ?
Related to the length of 'ifelse' result, I want to say that "example of different return modes" in ?ifelse led me to perceive a wrong thing in the past.
## example of different return modes:
yes <- 1:3
no <- pi^(0:3)
typeof(ifelse(NA, yes, no)) # logical
typeof(ifelse(TRUE, yes, n...
2012 Jan 19
3
Establishing groups using something other than ifelse()
...ng variable (z). This is my base
data. Now I know that there is a higher order level of grouping that
exist for my group variable. So what I want to do is create a new
column that express that higher order level of grouping based on
values in the sub-group (z in this case). In the past I have used
ifelse() but this tends to get fairly redundant and messy with a large
amount of sub-groupings (z). I've created a sample dataset below. Can
anyone recommend a better way of achieving what I am currently
achieving with ifelse()? A long series of ifelse statements makes me
think that there is something...
2010 Dec 09
1
set dataframe field value from lookup table
...ore cogent phrasing of a previous post. I'm
trying to compute a z-score to rows in a large dataframe based on values in
another dataframe. Here's the script (that does not work). 2 questons,
1) Anyone know of a more elegant way to calculate the "rounded" age value
than the nested ifelse's I've used?
2) how to reference the lookup table based on computed indices?
Thanks
Jon
# Define tables
DSTzlook <-
read.table("/Users/jween/Documents/ResearchProjects/ABC/data/DSTz.txt",
header=TRUE, sep="\t", na.strings="NA", dec=".", strip.w...
2005 Jun 01
2
A suggestion to improve ifelse behaviour with vector yes/noarguments
...wrote:
> > On Tue, 31 May 2005, Duncan Murdoch wrote:
> >
> >
> >>M??kinen Jussi wrote:
> >>
> >>>Dear All,
> >>>
> >>>I luckily found the following feature (or problem) when tried to
> >>>apply
> >>>ifelse-function to an ordered data.
> >>>
> >>>
> >>>
> >>>>test <- c(TRUE, TRUE, TRUE, FALSE, FALSE, FALSE, FALSE) ifelse(test,
> >>>>0, 1:4)
> >>>
> >>>[1] 0 0 0 4 1 2 3
> >>>
> >
> > &...
2023 Aug 10
2
Expresión en un objeto
...gamos que tenemos tres variables:
V1 <- c (47, 71, 41, 23, 83, 152, 82, 8, 160, 18)
V2a <- c (NA, 36, 15, 5, 56, 18, NA, 5, NA, 5)
V2b <- c (37, NA, 15, NA, NA, NA, 90, NA, 161, NA)
Supongamos que tengo la expresión (que no puedo asignarlo a ninguna variable):
(ifelse (is.na (V2a) & ! is.na (V2b), V2b, V2a))
Supongamos que tengo que utilizar esta expresión dos o más veces y no
puedo utilizar ni un xapply () ni un bucle. Por ejemplo:
V3 <- (((ifelse (is.na (V2a) & ! is.na (V2b), V2b, V2a)) - V1)/V1)*100
V4 <- ifelse (! is.na ((((ifelse (is.na (V...