Hello Rui and Gerrit and Sarah, Thank you very much for your help. I finally got it to work!
Have a great day! Dagmar Am 29.10.2015 um 08:38 schrieb ruipbarra...@sapo.pt: > > Hello, > > Try the following. > > > newframe <- myframe > newframe$Hungertype <- with(myframe, ifelse(Hunger <= 1, NA, > ifelse(1 < Hunger & Hunger <= 2, "Hunger", > ifelse(Hunger < 3, "bigHUnger", > "verybigHunger") ))) > > > Note that the new column is of type character but that in your example > myframeresult it is of type factor. > > Hope this helps, > > Rui Barradas > > Citando Dagmar <ramga...@gmx.net <mailto:ramga...@gmx.net>>: > >> Dear Sarah, (dear All), >> Thank you for trying to help! You are right, that I want to add a >> column to my dataframe with a corresponding value to the Hunger column. >> Your solution is basically correct in the result but my data are a >> little more complicated. Maybe that example describes it better: >> >> myframe <- data.frame (ID=c("Ernie", "Ernie", "Ernie", "Bert", >> "Bert","Bert", "Duck"), Hunger=c(1.2,1.3,1.1,2.1,2.2,1.4,3.3) ) >> myframe >> >> # Now I want to add a column which says "hunger" for values between >> 1.0 - 2; >> #"big Hunger" for >2 $ <=3, "very big Hunger" for >3 >> # so that the result looks somewhat like that: >> myframeresult <- data.frame (ID=c("Ernie", "Ernie", "Ernie", "Bert", >> "Bert","Bert", "Duck"), >> Hunger=c(1.2,1.3,1.1,2.1,2.2,1.4,3.3),Hungertype =c("Hunger", >> "Hunger", "Hunger", "bigHunger", "bigHunger", >> "Hunger","verybigHunger" ) ) >> myframeresult >> >> Does anyone know the solution? >> Thanks in advance for trying, >> Dagmar >> >> >> Am 28.10.2015 um 20:54 schrieb Sarah Goslee: >> >>> If I'm reading this correctly, you want to add a column to your >>> dataframe with a name corresponding to the value in the Hunger column. >>> >>> myframe <- data.frame (ID=c("Ernie", "Ernie", "Ernie", "Bert", >>> "Bert","Bert", "Duck"), Hunger=c(1,1,1,2,2,1,3) ) >>> >>> myframe$Hungertype <- c("none", "bighunger", >>> "verybighunger")[myframe$Hunger] >>> >>> ID Hunger Hungertype >>> 1 Ernie 1 none >>> 2 Ernie 1 none >>> 3 Ernie 1 none >>> 4 Bert 2 bighunger >>> 5 Bert 2 bighunger >>> 6 Bert 1 none >>> 7 Duck 3 verybighunger >>> >>> Then you can subset it to remove low values, sort it, etc. >>> >>> On Wed, Oct 28, 2015 at 8:20 AM, Dagmar Cimiotti >>> <dagmar.cimio...@gmx.de <mailto:dagmar.cimio...@gmx.de>> wrote: >>> >>>> Hello, >>>> It must be very easy. >>>> >>>> I have data like this: >>>> myframe <- data.frame (ID=c("Ernie", "Ernie", "Ernie", "Bert", >>>> "Bert","Bert", "Duck"), Hunger=c(1,1,1,2,2,1,3) ) >>>> myframe >>>> bighunger <- subset (myframe, myframe$Hunger>=2 &myframe$Hunger <3 ) >>>> bighunger >>>> verybighunger <- subset(myframe,myframe$Hunger>=3) >>>> verybighunger >>>> hungry <- rbind (bighunger=bighunger,very=verybighunger) >>>> hungry >>>> >>>> BUT I want a result like this: >>>> myframesresult <- data.frame(Hunger=c("bighunger","bighunger","very"), >>>> ID=c("Bert", "Bert", "duck"), Hunger=c(2,2,3)) >>>> myframesresult >>>> >>>> Where is my mistake? >>>> Very many thanks in advance!! >>>> Dagmar >>>> >> ______________________________________________ >> R-help@r-project.org <mailto:R-help@r-project.org> mailing list -- To >> UNSUBSCRIBE and more, see >> https://stat.ethz.ch/mailman/listinfo/r-help >> PLEASE do read the posting guide >> http://www.R-project.org/posting-guide.htmland provide commented, >> minimal, self-contained, reproducible code. > > [[alternative HTML version deleted]] ______________________________________________ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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.