Displaying 20 results from an estimated 10000 matches similar to: "rep within an ifelse statment"
2003 Oct 07
3
Problem getting an ifelse statment to work
This is a "long" way; i.e., not necessarily efficient:
> qs2
[1] 2 1 1 4 4 4 1 1 1 4 2 4 3 1 4 3 3 2 4 3
> qs9
[1] 4 4 1 3 4 3 1 3 1 4 1 2 3 3 4 4 1 4 2 3
> decision <- function(a, b) {
+ if (a == 1 || b == 1) return(1)
+ if (a == 2 || b == 2) return(2)
+ if (a == 3 || b == 3) return(3)
+ if (a == 4 || b == 4) return(4)
+ NA
+ }
> mapply(decision,
2003 Mar 07
1
Type IV sum of squares
To anyone who can help:
I am trying to do an analysis of variance with a very unbalanced design and a few empty cells. How do I get R to use type III and more importantly type IV sums of squares?
Thanks
Deven Hamilton
Department of Sociology
University of Washington
2005 Nov 27
5
creating a factor from other factors and ifelse
Hi,
Given
> sec98 <- factor(rep(1:2,3), labels=c("A", "B"))
> sec99 <- factor(rep(2:1,3), labels=c("A", "B"))
> sec99[c(2,5)] <- NA
> sec00 <- factor( c( rep(1,3), rep(2,3) ), labels=c("A", "B"))
> sec00[c(2,4)] <- NA
> sec1 <- ifelse(!is.na(sec99), sec99,
ifelse(!is.na(sec00), sec00, NA
2005 May 02
1
newbie ifelse matrix question
Hi all,
I have time series data in a matrix format 20 rows x 205 columns and have been trying to replace outliers with NA. My first column contains the outliers threshold (3 Standard deviations) for each row - here's a bit of the first row
sd3 V1 V2 V3 V4 V5 V6 V7 V8 V9
1 13.03267 1797157 75 84 58 -1.958649 0.048775 2.056198 8.063622
2013 Jan 08
1
incrementation within ifelse
Dear R-helper,
I am working on a very large data frame and I am trying to add a new column
and write in it with certain conditions. I have try to use this code with
the data frame p :
ID = 0
p[,"newColumn"]<-
ifelse (p$flagFoehn3_durr == 1,
ifelse(p$Guetsch == 0,
ID <<- ID ++
,
ID
)
,
0
)
What I am trying to do
2011 Mar 01
3
inefficient ifelse() ?
dear R experts---
t <- 1:30
f <- function(t) { cat("f for", t, "\n"); return(2*t) }
g <- function(t) { cat("g for", t, "\n"); return(3*t) }
s <- ifelse( t%%2==0, g(t), f(t))
shows that the ifelse function actually evaluates both f() and g() for
all values first, and presumably then just picks left or right results
based on t%%2.
2010 Mar 10
1
ifelse logic and multiple assignments
I'm a fairly new R user and while I have a solution to my problem I'm
wondering if there is a better one.
In SAS it's common to use if/then 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
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, no)) # integer
typeof(ifelse(FALSE, yes, no)) # double
As
2005 Jun 22
2
Trouble with ifelse and if statement (PR#7962)
Full_Name: Woolton Lee
Version: 2.1
OS: windows
Submission from: (NULL) (128.118.224.46)
I did the following ('g' 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
2005 Jun 01
2
A suggestion to improve ifelse behaviour with vector yes/noarguments
> Thomas Lumley 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.
> >>>
> >>>
>
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
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$status=='sanAa',1,0)
b2$sanDk <-
2009 Jun 24
5
How to avoid ifelse statement converting factor to character
Hi R-helpers,
Please see the below R output.
The problem is that after running the ifelse statement, data$SOCIAL_STATUS
is converted from a factor to a character.
Is there some way I can avoid this conversion?
Thanks in advance, Mark Na
> str(data)
'data.frame': 2100 obs. of 11 variables:
$ DATE : Factor w/ 5 levels "4-Jun-09","7-May-09",..: 1 1 1 1 1
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 proposed ifelse do
not intersect.
The patch does not intend to change the
2007 Dec 20
1
ifelse problem
Could someone help me with the following code snippet. The results are not
what I expect:
> Sheet1$Claims[1:10]
[1] NA 1 2 NA NA NA NA NA NA NA
> Sheet1[1:10,"SubmissionStatus"]
[1] Declined Bound Bound Bound Bound Bound Declined
Dead Declined
[10] Not Taken
Levels: Bound Dead Declined Not Taken
> Sheet1$Claimsnum <- NA
>
2008 Aug 27
1
ifelse() fill order and recycling rules [Sec=Unclassified]
Hi all,
Using R v2.7.1, platform i386-pc-mingw32
Can someone please shed some light on the behaviour of ifelse() for me?
My intent is to calc relative proportions of z$b, at the same time
subsetting z$b based on z$a. I could attack the problem other ways
(suggestions welcome) but I am also intrigued by the _order_ in which
ifelse seems to assign values, and how recycling works. For instance,
2010 Mar 17
2
How to use "ifelse" to generate random value from a distribution
I need use different parameters of distribution for different case to
generate random value, but I use ifelse, the generated value is fixed
without change.
Here is example
data1
y x
1 1 2
2 2 1
3 3 2
4 4 3
5 5 3
6 6 1
7 7 2
8 8 1
9 9 1
10 10 3
11 11 3
12 12 2
ifelse(data1$x==1,rnorm(1,2,1),ifelse(data1$x==2,rnorm(1,-2,1),rnorm(1,110,1)))
[1] -1.8042172 0.8478681
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:
2012 Jan 19
3
Establishing groups using something other than ifelse()
Hello all,
This is one of those "Is there a better way to do this questions". Say
I have a dataframe (df) with a grouping 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
2010 Dec 23
1
with(data.frame,ifelse(___,___,___))
Hello, All,
Mac OS 10.6.5
R64 2.11.1
This works as expected:
f1 = c(0.084, 0.099, 0)
data= data.frame(f1)
data$f1=with(data,ifelse(f1==0, 0.0001, f1))
data
f1
1 0.0840
2 0.0990
3 0.0001
Substituting ''f1==0'' with ''T'' produces the expected result:
f1 = c(0.084, 0.099, 0)
data= data.frame(f1)
data$f1=with(data,ifelse(T, 0.0001, f1))
data
f1
1 1e-04