|
Hi all,
It seems like I cannot use normal 'if' for data frames. What would be the best way to do the following. if data$col1='high' data$col2='H' else if data$col1='Neutral' data$col2='N' else if data$col='low' data$col2='L' else #chuch a warning? Note that col2 was not an existing column and was newly assigned for this task. Thanks, Sachin [[alternative HTML version deleted]] ______________________________________________ [hidden email] 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. |
|
Hi,
Try this: dat1<-data.frame(col1=c(rep("high",3),rep("Neutral",3),rep("low",4))) dat1$col2<-ifelse(dat1$col1=="high",dat1$col2<-"H",ifelse(dat1$col1=="Neutral",dat1$col2<-"N","L")) dat1 col1 col2 1 high H 2 high H 3 high H 4 Neutral N 5 Neutral N 6 Neutral N 7 low L 8 low L 9 low L 10 low L A.K. ----- Original Message ----- From: Sachinthaka Abeywardana <[hidden email]> To: [hidden email] Cc: Sent: Sunday, August 12, 2012 8:43 PM Subject: [R] if else elseif for data frames Hi all, It seems like I cannot use normal 'if' for data frames. What would be the best way to do the following. if data$col1='high' data$col2='H' else if data$col1='Neutral' data$col2='N' else if data$col='low' data$col2='L' else #chuch a warning? Note that col2 was not an existing column and was newly assigned for this task. Thanks, Sachin [[alternative HTML version deleted]] ______________________________________________ [hidden email] 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. ______________________________________________ [hidden email] 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. |
|
The thing is I have about 10 cases. I saw the ifelse statement but was
wondering if there was a cleaner method of doing it. The coding will get really messy when I write all 10 cases. Cheers, Sachin On Mon, Aug 13, 2012 at 11:04 AM, arun <[hidden email]> wrote: > Hi, > Try this: > dat1<-data.frame(col1=c(rep("high",3),rep("Neutral",3),rep("low",4))) > > dat1$col2<-ifelse(dat1$col1=="high",dat1$col2<-"H",ifelse(dat1$col1=="Neutral",dat1$col2<-"N","L")) > dat1 > col1 col2 > 1 high H > 2 high H > 3 high H > 4 Neutral N > 5 Neutral N > 6 Neutral N > 7 low L > 8 low L > 9 low L > 10 low L > > A.K. > > > > > ----- Original Message ----- > From: Sachinthaka Abeywardana <[hidden email]> > To: [hidden email] > Cc: > Sent: Sunday, August 12, 2012 8:43 PM > Subject: [R] if else elseif for data frames > > Hi all, > > It seems like I cannot use normal 'if' for data frames. What would be the > best way to do the following. > > if data$col1='high' > data$col2='H' > else if data$col1='Neutral' > data$col2='N' > else if data$col='low' > data$col2='L' > else > #chuch a warning? > > > Note that col2 was not an existing column and was newly assigned for this > task. > > Thanks, > Sachin > > [[alternative HTML version deleted]] > > ______________________________________________ > [hidden email] 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. > > [[alternative HTML version deleted]] ______________________________________________ [hidden email] 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. |
|
Hi,
Try this, set.seed(1) dat2<-data.frame(col1=c(sample(c("high","Neutral","low"),10,replace=TRUE)),col2=rep(NA,10)) dat2$col2[dat2$col1=="high"]<-"H" dat2$col2[dat2$col1=="Neutral"]<-"N" dat2$col2[dat2$col1=="low"]<-"L" dat2 # col1 col2 #1 high H #2 Neutral N #3 Neutral N #4 low L #5 high H #6 low L #7 low L #8 Neutral N #9 Neutral N #10 high H Not sure if this is okay for you, A.K.rr A.K. ________________________________ From: Sachinthaka Abeywardana <[hidden email]> To: arun <[hidden email]> Cc: R help <[hidden email]> Sent: Sunday, August 12, 2012 9:07 PM Subject: Re: [R] if else elseif for data frames The thing is I have about 10 cases. I saw the ifelse statement but was wondering if there was a cleaner method of doing it. The coding will get really messy when I write all 10 cases. Cheers, Sachin On Mon, Aug 13, 2012 at 11:04 AM, arun <[hidden email]> wrote: Hi, >Try this: >dat1<-data.frame(col1=c(rep("high",3),rep("Neutral",3),rep("low",4))) > dat1$col2<-ifelse(dat1$col1=="high",dat1$col2<-"H",ifelse(dat1$col1=="Neutral",dat1$col2<-"N","L")) >dat1 > col1 col2 >1 high H >2 high H >3 high H >4 Neutral N >5 Neutral N >6 Neutral N >7 low L >8 low L >9 low L >10 low L > >A.K. > > > > > >----- Original Message ----- >From: Sachinthaka Abeywardana <[hidden email]> >To: [hidden email] >Cc: >Sent: Sunday, August 12, 2012 8:43 PM >Subject: [R] if else elseif for data frames > >Hi all, > >It seems like I cannot use normal 'if' for data frames. What would be the >best way to do the following. > >if data$col1='high' > data$col2='H' >else if data$col1='Neutral' > data$col2='N' >else if data$col='low' > data$col2='L' >else > #chuch a warning? > > >Note that col2 was not an existing column and was newly assigned for this >task. > >Thanks, >Sachin > > [[alternative HTML version deleted]] > >______________________________________________ >[hidden email] 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. > > ______________________________________________ [hidden email] 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. |
|
HI, Much better solution: library(car) set.seed(1) dat2<-data.frame(col1=c(sample(c("high","Neutral","low"),10,replace=TRUE)),col2=rep(NA,10)) x<-dat2$col1 dat2$col2<-recode(x,'"high"="H";"Neutral"="N";"low"="L"') dat2 # col1 col2 #1 high H #2 Neutral N #3 Neutral N #4 low L #5 high H #6 low L #7 low L #8 Neutral N #9 Neutral N #10 high H A.K. ________________________________ From: Sachinthaka Abeywardana <[hidden email]> To: arun <[hidden email]> Sent: Sunday, August 12, 2012 9:18 PM Subject: Re: [R] if else elseif for data frames yup looks good. thanks Sachin On Mon, Aug 13, 2012 at 11:17 AM, arun <[hidden email]> wrote: Hi, > >Try this, > >set.seed(1) > dat2<-data.frame(col1=c(sample(c("high","Neutral","low"),10,replace=TRUE)),col2=rep(NA,10)) >dat2$col2[dat2$col1=="high"]<-"H" > dat2$col2[dat2$col1=="Neutral"]<-"N" > dat2$col2[dat2$col1=="low"]<-"L" > dat2 ># col1 col2 >#1 high H >#2 Neutral N >#3 Neutral N >#4 low L >#5 high H >#6 low L >#7 low L >#8 Neutral N >#9 Neutral N >#10 high H > > >Not sure if this is okay for you, >A.K.rr > > >A.K. > >________________________________ >From: Sachinthaka Abeywardana <[hidden email]> >To: arun <[hidden email]> >Cc: R help <[hidden email]> >Sent: Sunday, August 12, 2012 9:07 PM >Subject: Re: [R] if else elseif for data frames > > > >The thing is I have about 10 cases. I saw the ifelse statement but was wondering if there was a cleaner method of doing it. The coding will get really messy when I write all 10 cases. > >Cheers, >Sachin > > >On Mon, Aug 13, 2012 at 11:04 AM, arun <[hidden email]> wrote: > >Hi, >>Try this: >>dat1<-data.frame(col1=c(rep("high",3),rep("Neutral",3),rep("low",4))) >> dat1$col2<-ifelse(dat1$col1=="high",dat1$col2<-"H",ifelse(dat1$col1=="Neutral",dat1$col2<-"N","L")) >>dat1 >> col1 col2 >>1 high H >>2 high H >>3 high H >>4 Neutral N >>5 Neutral N >>6 Neutral N >>7 low L >>8 low L >>9 low L >>10 low L >> >>A.K. >> >> >> >> >> >>----- Original Message ----- >>From: Sachinthaka Abeywardana <[hidden email]> >>To: [hidden email] >>Cc: >>Sent: Sunday, August 12, 2012 8:43 PM >>Subject: [R] if else elseif for data frames >> >>Hi all, >> >>It seems like I cannot use normal 'if' for data frames. What would be the >>best way to do the following. >> >>if data$col1='high' >> data$col2='H' >>else if data$col1='Neutral' >> data$col2='N' >>else if data$col='low' >> data$col2='L' >>else >> #chuch a warning? >> >> >>Note that col2 was not an existing column and was newly assigned for this >>task. >> >>Thanks, >>Sachin >> >> [[alternative HTML version deleted]] >> >>______________________________________________ >>[hidden email] 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. >> >> > ______________________________________________ [hidden email] 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. |
|
In reply to this post by Sachinthaka Abeywardana
It seems to me that the "recode()" function from the "car" package is what you need. cheers, Rolf Turner On 13/08/12 13:07, Sachinthaka Abeywardana wrote: > The thing is I have about 10 cases. I saw the ifelse statement but was > wondering if there was a cleaner method of doing it. The coding will get > really messy when I write all 10 cases. > > Cheers, > Sachin > > On Mon, Aug 13, 2012 at 11:04 AM, arun <[hidden email]> wrote: > >> Hi, >> Try this: >> dat1<-data.frame(col1=c(rep("high",3),rep("Neutral",3),rep("low",4))) >> >> dat1$col2<-ifelse(dat1$col1=="high",dat1$col2<-"H",ifelse(dat1$col1=="Neutral",dat1$col2<-"N","L")) >> dat1 >> col1 col2 >> 1 high H >> 2 high H >> 3 high H >> 4 Neutral N >> 5 Neutral N >> 6 Neutral N >> 7 low L >> 8 low L >> 9 low L >> 10 low L >> >> A.K. >> >> >> >> >> ----- Original Message ----- >> From: Sachinthaka Abeywardana <[hidden email]> >> To: [hidden email] >> Cc: >> Sent: Sunday, August 12, 2012 8:43 PM >> Subject: [R] if else elseif for data frames >> >> Hi all, >> >> It seems like I cannot use normal 'if' for data frames. What would be the >> best way to do the following. >> >> if data$col1='high' >> data$col2='H' >> else if data$col1='Neutral' >> data$col2='N' >> else if data$col='low' >> data$col2='L' >> else >> #chuch a warning? >> >> >> Note that col2 was not an existing column and was newly assigned for this >> task. >> >> Thanks, >> Sachin >> >> [[alternative HTML version deleted]] >> >> ______________________________________________ >> [hidden email] 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. >> >> > [[alternative HTML version deleted]] > > ______________________________________________ > [hidden email] 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. > ______________________________________________ [hidden email] 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. |
|
In reply to this post by Sachinthaka Abeywardana
You might look at the 'recode' that is part of the 'car' package. You
can also setup a dataframe with two columns; one with the current value and one with the new value you want, then you can use 'merge' or 'match' to create your new column. If you have provided some sample data, I could have provided examples, but now left as an exercise for the reader. On Sun, Aug 12, 2012 at 9:07 PM, Sachinthaka Abeywardana <[hidden email]> wrote: > The thing is I have about 10 cases. I saw the ifelse statement but was > wondering if there was a cleaner method of doing it. The coding will get > really messy when I write all 10 cases. > > Cheers, > Sachin > > On Mon, Aug 13, 2012 at 11:04 AM, arun <[hidden email]> wrote: > >> Hi, >> Try this: >> dat1<-data.frame(col1=c(rep("high",3),rep("Neutral",3),rep("low",4))) >> >> dat1$col2<-ifelse(dat1$col1=="high",dat1$col2<-"H",ifelse(dat1$col1=="Neutral",dat1$col2<-"N","L")) >> dat1 >> col1 col2 >> 1 high H >> 2 high H >> 3 high H >> 4 Neutral N >> 5 Neutral N >> 6 Neutral N >> 7 low L >> 8 low L >> 9 low L >> 10 low L >> >> A.K. >> >> >> >> >> ----- Original Message ----- >> From: Sachinthaka Abeywardana <[hidden email]> >> To: [hidden email] >> Cc: >> Sent: Sunday, August 12, 2012 8:43 PM >> Subject: [R] if else elseif for data frames >> >> Hi all, >> >> It seems like I cannot use normal 'if' for data frames. What would be the >> best way to do the following. >> >> if data$col1='high' >> data$col2='H' >> else if data$col1='Neutral' >> data$col2='N' >> else if data$col='low' >> data$col2='L' >> else >> #chuch a warning? >> >> >> Note that col2 was not an existing column and was newly assigned for this >> task. >> >> Thanks, >> Sachin >> >> [[alternative HTML version deleted]] >> >> ______________________________________________ >> [hidden email] 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. >> >> > > [[alternative HTML version deleted]] > > ______________________________________________ > [hidden email] 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. -- Jim Holtman Data Munger Guru What is the problem that you are trying to solve? Tell me what you want to do, not how you want to do it. ______________________________________________ [hidden email] 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. |
|
Maybe the cut function?
?cut --------------------------------------------------------------------------- Jeff Newmiller The ..... ..... Go Live... DCN:<[hidden email]> Basics: ##.#. ##.#. Live Go... Live: OO#.. Dead: OO#.. Playing Research Engineer (Solar/Batteries O.O#. #.O#. with /Software/Embedded Controllers) .OO#. .OO#. rocks...1k --------------------------------------------------------------------------- Sent from my phone. Please excuse my brevity. jim holtman <[hidden email]> wrote: >You might look at the 'recode' that is part of the 'car' package. You >can also setup a dataframe with two columns; one with the current >value and one with the new value you want, then you can use 'merge' or >'match' to create your new column. If you have provided some sample >data, I could have provided examples, but now left as an exercise for >the reader. > >On Sun, Aug 12, 2012 at 9:07 PM, Sachinthaka Abeywardana ><[hidden email]> wrote: >> The thing is I have about 10 cases. I saw the ifelse statement but >was >> wondering if there was a cleaner method of doing it. The coding will >get >> really messy when I write all 10 cases. >> >> Cheers, >> Sachin >> >> On Mon, Aug 13, 2012 at 11:04 AM, arun <[hidden email]> >wrote: >> >>> Hi, >>> Try this: >>> >dat1<-data.frame(col1=c(rep("high",3),rep("Neutral",3),rep("low",4))) >>> >>> >dat1$col2<-ifelse(dat1$col1=="high",dat1$col2<-"H",ifelse(dat1$col1=="Neutral",dat1$col2<-"N","L")) >>> dat1 >>> col1 col2 >>> 1 high H >>> 2 high H >>> 3 high H >>> 4 Neutral N >>> 5 Neutral N >>> 6 Neutral N >>> 7 low L >>> 8 low L >>> 9 low L >>> 10 low L >>> >>> A.K. >>> >>> >>> >>> >>> ----- Original Message ----- >>> From: Sachinthaka Abeywardana <[hidden email]> >>> To: [hidden email] >>> Cc: >>> Sent: Sunday, August 12, 2012 8:43 PM >>> Subject: [R] if else elseif for data frames >>> >>> Hi all, >>> >>> It seems like I cannot use normal 'if' for data frames. What would >be the >>> best way to do the following. >>> >>> if data$col1='high' >>> data$col2='H' >>> else if data$col1='Neutral' >>> data$col2='N' >>> else if data$col='low' >>> data$col2='L' >>> else >>> #chuch a warning? >>> >>> >>> Note that col2 was not an existing column and was newly assigned for >this >>> task. >>> >>> Thanks, >>> Sachin >>> >>> [[alternative HTML version deleted]] >>> >>> ______________________________________________ >>> [hidden email] 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. >>> >>> >> >> [[alternative HTML version deleted]] >> >> ______________________________________________ >> [hidden email] 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. > > > >-- >Jim Holtman >Data Munger Guru > >What is the problem that you are trying to solve? >Tell me what you want to do, not how you want to do it. > >______________________________________________ >[hidden email] 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. ______________________________________________ [hidden email] 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. |
|
In reply to this post by Sachinthaka Abeywardana
On Aug 12, 2012, at 5:43 PM, Sachinthaka Abeywardana wrote: > Hi all, > > It seems like I cannot use normal 'if' for data frames. What would > be the > best way to do the following. > > if data$col1='high' > data$col2='H' > else if data$col1='Neutral' > data$col2='N' > else if data$col='low' > data$col2='L' > else > #chuch a warning? > > > Note that col2 was not an existing column and was newly assigned for > this > task. Using arun's example, dat1: dat1$col2 <- c("H","N", "L","warn")[ match(dat1$col1, c("low", "Neutral", "high"), nomatch=4 )] > dat1 col1 col2 1 high L 2 high L 3 high L 4 Neutral N 5 Neutral N 6 Neutral N 7 low H 8 low H 9 low H 10 low H Nested ifelse constructions would be quite inefficient. I'm not even sure that a 10-deep ifelse construction woul be acceptable to the interpreter. At one point I thought I read something about a nesting depth of 7 as being a limit. (But many years have passed since that reading and it's possible it's just a manufactured memory or that the limit has been raised.) -- David Winsemius, MD Alameda, CA, USA ______________________________________________ [hidden email] 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. |
|
In reply to this post by Sachinthaka Abeywardana
On Sun, Aug 12, 2012 at 8:07 PM, Sachinthaka Abeywardana
<[hidden email]> wrote: > The thing is I have about 10 cases. I saw the ifelse statement Note that there is no "ifelse" statement: there is only nested if/else of forms if else if else if else .... > but was > wondering if there was a cleaner method of doing it. The coding will get > really messy when I write all 10 cases. > > Cheers, > Sachin > Possibly look at ?switch Michael ______________________________________________ [hidden email] 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. |
|
In reply to this post by Sachinthaka Abeywardana
Hi
What about creating a new column with same factor and just chase its levels. dat2$col2<-dat2$col1 > levels(dat2$col2) [1] "high" "low" "Neutral" > levels(dat2$col2)<-c("H", "L","N") > dat2 col1 col2 1 high H 2 Neutral N 3 Neutral N 4 low L 5 high H 6 low L 7 low L 8 Neutral N 9 Neutral N 10 high H Regards Petr > > > The thing is I have about 10 cases. I saw the ifelse statement but was > wondering if there was a cleaner method of doing it. The coding will > get really messy when I write all 10 cases. > > Cheers, > Sachin > > On Mon, Aug 13, 2012 at 11:04 AM, arun <[hidden email]> wrote: > > > Hi, > > Try this: > > dat1<-data.frame(col1=c(rep("high",3),rep("Neutral",3),rep("low",4))) > > > > > > dat1$col2<-ifelse(dat1$col1=="high",dat1$col2<- > "H",ifelse(dat1$col1==" > > Neutral",dat1$col2<-"N","L")) > > dat1 > > col1 col2 > > 1 high H > > 2 high H > > 3 high H > > 4 Neutral N > > 5 Neutral N > > 6 Neutral N > > 7 low L > > 8 low L > > 9 low L > > 10 low L > > > > A.K. > > > > > > > > > > ----- Original Message ----- > > From: Sachinthaka Abeywardana <[hidden email]> > > To: [hidden email] > > Cc: > > Sent: Sunday, August 12, 2012 8:43 PM > > Subject: [R] if else elseif for data frames > > > > Hi all, > > > > It seems like I cannot use normal 'if' for data frames. What would be > > the best way to do the following. > > > > if data$col1='high' > > data$col2='H' > > else if data$col1='Neutral' > > data$col2='N' > > else if data$col='low' > > data$col2='L' > > else > > #chuch a warning? > > > > > > Note that col2 was not an existing column and was newly assigned for > > this task. > > > > Thanks, > > Sachin > > > > [[alternative HTML version deleted]] > > > > ______________________________________________ > > [hidden email] 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. > > > > > > [[alternative HTML version deleted]] > > ______________________________________________ > [hidden email] 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. ______________________________________________ [hidden email] 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. |
| Powered by Nabble | Edit this page |
