Steve Matco
2010-Feb-26 19:31 UTC
[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 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.?The existing variable?in my dataframe is?a character variable named DOW which contains abbreviated day names (SAT, SUN, MON.....FRI). I want to add a numerical variable named DOW1 to my dataframe that will?take?on the value?1 if DOW equals "SAT", 2 if DOW equals "SUN", 3 if DOW equals "MON",.....,7 if DOW equals "FRI". I? know this must be a simple problem but I have searched everywhere and tried everything I could think of. Any help would be greatly appreciated. Thank you, Mike
Erik Iverson
2010-Feb-26 19:38 UTC
[R] How to add a variable to a dataframe whose values are conditional upon the values of an existing variable
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", 1, ifelse(my.df$DOW == "SUN", 2, ifelse(my.df$DOW == "MON", 3, ifelse(my.df$DOW == "TUE", 4, ifelse(my.df$DOW == "WED", 5, ifelse(my.df$DOW == "THU", 6, 7)))))) (don't know if the number of closing ")" is right, but you get the idea... Erik 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 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. The existing variable in my dataframe is a character variable named DOW which contains abbreviated day names (SAT, SUN, MON.....FRI). I want to add a numerical variable named DOW1 to my dataframe that will take on the value 1 if DOW equals "SAT", 2 if DOW equals "SUN", 3 if DOW equals "MON",.....,7 if DOW equals "FRI". > I know this must be a simple problem but I have searched everywhere and tried everything I could think of. Any help would be greatly appreciated. > > Thank you, > > Mike > > > > > ______________________________________________ > R-help at 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.
Henrique Dallazuanna
2010-Feb-26 19:38 UTC
[R] How to add a variable to a dataframe whose values are conditional upon the values of an existing variable
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 <h20strider at yahoo.com> 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 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.?The existing variable?in my dataframe is?a character variable named DOW which contains abbreviated day names (SAT, SUN, MON.....FRI). I want to add a numerical variable named DOW1 to my dataframe that will?take?on the value?1 if DOW equals "SAT", 2 if DOW equals "SUN", 3 if DOW equals "MON",.....,7 if DOW equals "FRI". > I? know this must be a simple problem but I have searched everywhere and tried everything I could think of. Any help would be greatly appreciated. > > Thank you, > > Mike > > > > > ______________________________________________ > R-help at 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. >-- Henrique Dallazuanna Curitiba-Paran?-Brasil 25? 25' 40" S 49? 16' 22" O
Andrew Miles
2010-Feb-26 19:46 UTC
[R] How to add a variable to a dataframe whose values are conditional upon the values of an existing variable
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=="cow", 1, 6) mat$g=ifelse(mat$t=="dog", 2, mat$g) mat$g=ifelse(mat$t=="chick", 3, mat$g) > mat t y g 1 cow 1 1 2 dog 3 2 3 chick 4 3 To days of the week would only be 7 statements. Andrew Miles Department of Sociology Duke University On Feb 26, 2010, at 2: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 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. The existing variable in my dataframe is a character > variable named DOW which contains abbreviated day names (SAT, SUN, > MON.....FRI). I want to add a numerical variable named DOW1 to my > dataframe that will take on the value 1 if DOW equals "SAT", 2 if > DOW equals "SUN", 3 if DOW equals "MON",.....,7 if DOW equals "FRI". > I know this must be a simple problem but I have searched everywhere > and tried everything I could think of. Any help would be greatly > appreciated. > > Thank you, > > Mike > > > > > ______________________________________________ > R-help at 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.
Daniel Malter
2010-Feb-26 19:48 UTC
[R] How to add a variable to a dataframe whose values are conditional upon the values of an existing variable
Hi, two approaches at least. a. a nested ifelse statement b. merging the original data frame with DOW in it with a data frame that holds the day "MON" to "SUN" and their indicators. Both approaches illustrated below: DOW=rep(c("MON","TUE"),100) #nested ifelse approach DOW.ind=ifelse(DOW=="MON",1,ifelse(DOW=="TUE",2,0)) #continue to nest ifelse statements for more days DOW.ind #merging approach day=c("MON","TUE") ind=c(1,2) ind.frame=data.frame(day,ind) merge(data.frame(DOW),ind.frame,by.x="DOW",by.y="day",all.x=T,all.y=F) HTH, Daniel ------------------------- cuncta stricte discussurus ------------------------- -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Steve Matco Sent: Friday, February 26, 2010 2:32 PM To: r-help at r-project.org 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 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.?The existing variable?in my dataframe is?a character variable named DOW which contains abbreviated day names (SAT, SUN, MON.....FRI). I want to add a numerical variable named DOW1 to my dataframe that will?take?on the value?1 if DOW equals "SAT", 2 if DOW equals "SUN", 3 if DOW equals "MON",.....,7 if DOW equals "FRI". I? know this must be a simple problem but I have searched everywhere and tried everything I could think of. Any help would be greatly appreciated. Thank you, Mike ______________________________________________ R-help at 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.
Henrique Dallazuanna
2010-Feb-26 19:52 UTC
[R] How to add a variable to a dataframe whose values are conditional upon the values of an existing variable
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 <h20strider at yahoo.com> 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 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.?The existing variable?in my dataframe is?a character variable named DOW which contains abbreviated day names (SAT, SUN, MON.....FRI). I want to add a numerical variable named DOW1 to my dataframe that will?take?on the value?1 if DOW equals "SAT", 2 if DOW equals "SUN", 3 if DOW equals "MON",.....,7 if DOW equals "FRI". > I? know this must be a simple problem but I have searched everywhere and tried everything I could think of. Any help would be greatly appreciated. > > Thank you, > > Mike > > > > > ______________________________________________ > R-help at 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. >-- Henrique Dallazuanna Curitiba-Paran?-Brasil 25? 25' 40" S 49? 16' 22" O
Greg Snow
2010-Feb-27 15:36 UTC
[R] How to add a variable to a dataframe whose values are conditional upon the values of an existing variable
Here is another approach (I think this is the simplest): daylkp <- c(SAT=1, SUN=2, MON=3, TUE=4, WED=5, THU=6, FRI=7) tmp.in <- sample( names(daylkp), 25, TRUE ) tmp.out <- daylkp[tmp.in] names(tmp.out) <- NULL # optional hope this helps, -- Gregory (Greg) L. Snow Ph.D. Statistical Data Center Intermountain Healthcare greg.snow at imail.org 801.408.8111> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r- > project.org] On Behalf Of Steve Matco > Sent: Friday, February 26, 2010 12:32 PM > To: r-help at r-project.org > 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 > 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.?The existing variable?in my dataframe is?a character > variable named DOW which contains abbreviated day names (SAT, SUN, > MON.....FRI). I want to add a numerical variable named DOW1 to my > dataframe that will?take?on the value?1 if DOW equals "SAT", 2 if DOW > equals "SUN", 3 if DOW equals "MON",.....,7 if DOW equals "FRI". > I? know this must be a simple problem but I have searched everywhere > and tried everything I could think of. Any help would be greatly > appreciated. > > Thank you, > > Mike > > > > > ______________________________________________ > R-help at 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.
David Freedman
2010-Feb-27 21:06 UTC
[R] How to add a variable to a dataframe whose values are conditional upon the values of an existing variable
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-to-a-dataframe-whose-values-are-conditional-upon-the-values-of-an-existing-vare-tp1571214p1572261.html Sent from the R help mailing list archive at Nabble.com.