[R] rbind - names in dataframe. Beginner Question

Sarah Goslee sarah.goslee at gmail.com
Wed Oct 28 20:54:44 CET 2015


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.cimiotti at 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



More information about the R-help mailing list