there's a recode function in the Hmisc package, but it's difficult (at least
for me) to find documentation for it
library(Hmisc)
week <- c('SAT', 'SUN', 'MON', 'FRI');
recode(week,c('SAT', 'SUN', 'MON', 'FRI'),1:4)
HTH
--
View this message in context:
http://n4.nabble.com/How-to-add-a-variable
> Subject: [R] How to add a variable to a dataframe whose values are
> conditional upon the values of an existing variable
>
> Hi everyone,
>
> I am at my wits end with what I believe would be considered simple by a
> more experienced R user. I want to know how to add a variable to a
You can work with factor also:
week <- c('SAT', 'SUN', 'MON', 'FRI')
factor(week, levels = week, labels = 1:4)
On Fri, Feb 26, 2010 at 4:31 PM, Steve Matco wrote:
> Hi everyone,
>
> I am at my wits end with what I believe would be considered simple by a more
> experienced R user. I want to know
W",by.y="day",all.x=T,all.y=F)
HTH,
Daniel
-
cuncta stricte discussurus
-
-Original Message-
From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On
Behalf Of Steve Matco
Sent: Friday, February 26, 2010 2:32 PM
To:
You could also try a series of simple ifelse statements. I just tried
the following and got it to work, though I am sure there is a faster
way.
t=c("cow", "dog", "chick")
y=c(1,3,4)
mat=cbind(t,y)
mat=as.data.frame(mat)
> mat
t y
1 cow 1
2 dog 3
3 chick 4
mat$g=ifelse(mat$t=="c
Try this:
sapply(c('SAT', 'SUN', 'MON', 'FRI'), switch, SAT = 1, SUN = 2, MON =
3, FRI = 4)
On Fri, Feb 26, 2010 at 4:31 PM, Steve Matco wrote:
> Hi everyone,
>
> I am at my wits end with what I believe would be considered simple by a more
> experienced R user. I want to know how to add a vari
You mention ifelse, so for completeness, I will show you a solution that
should work with that. There are other plenty of other possibilities
though, I am sure. The follow is not tested..
Assume 'my.df' is your data.frame, containing a variable "DOW".
my.df$DOW1 <- ifelse(my.df$DOW == "SAT",
Hi everyone,
I am at my wits end with what I believe would be considered simple by a more
experienced R user. I want to know how to add a variable to a dataframe whose
values are conditional on the values of an existing variable. I can't seem to
make an ifelse statement work for my situation. T
8 matches
Mail list logo