Jeff,
Even though the solutions from the previous responders are good enough
for my current situation, the principle you just raised will be
definitely beneficial to your future work. Thanks a lot for sharing
the insights!
Gang
On Thu, Jul 17, 2014 at 12:06 PM, Jeff Newmiller
wrote:
> You ask a
You ask about generic methods for introducing alternate values for
factors, and some of the other responses address this quite efficiently.
However, a factor has meaning only within one vector at a time, since
another vector may have additional values or missing values relative to
the first vect
Thanks a lot for the quick and elegant solutions, Sarah, Bill and
Petr! I really appreciate it, including the suggestion of setting a
random seed. Have a nice day!
Gang
On Thu, Jul 17, 2014 at 11:15 AM, Sarah Goslee wrote:
> What about:
>
> d$var <- c(8, 11, 3, 2)[d$fac]
>
> Side note: it's much
Hi
depends if you want the new column as factor or numeric.
d$fac2<-d$fac
I did not use var as it is R function.
levels(d$fac2)<-c(8,11,3,2)
d$fac2
[1] 3 11 8 8 2 8 11 11 8 8
Levels: 8 11 3 2
You can change it to character or numeric if you wish.
as.character(d$fac2)
[1] "3" "11" "8"
What about:
d$var <- c(8, 11, 3, 2)[d$fac]
Side note: it's much appreciated that you included data and a clear
problem statement. If you use
set.seed(123)
before your call to sample(), everyone who tries it will get the same
fac that you do. Otherwise we all get something different. Or just
gener
One way is to use a vector with names to do the mapping:
> mapVector <- c(A=8, B=11, C=3, D=2)
> mapVector[as.character(d$fac)]
B B D A C D C B B B
11 11 2 8 3 2 3 11 11 11
> # you may want to wrap this with unname()
> d$mappedFac <- mapVector[as.character(d$fac)]
> d
x y fac map
6 matches
Mail list logo