[R] rbind - names in dataframe. Beginner Question
Dagmar
Ramgad82 at gmx.net
Thu Oct 29 08:18:14 CET 2015
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.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