简体   繁体   中英

Reshape aggregated rows to new columns, categorical data

I am trying to use R to aggregate rows to columns. Here is a sample of my dataset.

age sex hash                                emotion     color
22  1   b17f9762462b37e7510f0e6d2534530d    Lonely      #006666
22  1   b17f9762462b37e7510f0e6d2534530d    Energetic   #66CC00
22  1   b17f9762462b37e7510f0e6d2534530d    Calm        #FFFFFF
22  1   b17f9762462b37e7510f0e6d2534530d    Angry       #FF0000
24  1   7bb50ca97a9b517239b39440a966d2f6    Calm        #006666
24  1   7bb50ca97a9b517239b39440a966d2f6    Excited     #0033cc
24  1   7bb50ca97a9b517239b39440a966d2f6    Empty/void  #999999
24  1   7bb50ca97a9b517239b39440a966d2f6    No emotion  #FF6600
26  1   209f1ba8ef86e855deccc0aae120825c    Comfortable #330066
21  1   b9e9309c0b1255a7efb2edf9ba66ae46    Energetic   #330099
21  1   b9e9309c0b1255a7efb2edf9ba66ae46    Happy       #330066
26  1   209f1ba8ef86e855deccc0aae120825c    No emotion  #FFCC00
26  1   209f1ba8ef86e855deccc0aae120825c    Calm        #006666
21  1   61debd3dea6d1aacce5c9fc7daec4fe5    Empty/void  #FFFFFF
21  1   b9e9309c0b1255a7efb2edf9ba66ae46    Calm        #006666
26  1   209f1ba8ef86e855deccc0aae120825c    No emotion  #339900
21  1   61debd3dea6d1aacce5c9fc7daec4fe5    Loved       #FF6600
26  1   209f1ba8ef86e855deccc0aae120825c    No emotion  #66CC00

What I want to do is get this:

age sex hash            #000000 #FF0000 ... #FFFFFF
22  1   8798tkojstwz9ei sad     happy   ... loved
...

One response is defined by the hash, associated data is age and sex.

I want to have each response as 1 instead of several columns. Each color should have it's own column and the associated emotion as value of that column.

The whole dataset has 13 colors, 20+ emotions and 1000+ responses. The dataset looks exactly as the sample and is stored in a mySQL database.

I have tried with reshape, but it doesn't play well with categorical data or I did not use the appropriate functions. Any ideas? It can include some mySQL preparation if needed. Java was here very slow and since I have 12k+ rows R sounds like the right thing for this.

Thank you.

using reshape2

dcast(dat,...~color,value.var='emotion')
  age sex                             hash #0033cc #006666     #330066   #330099   #339900   #66CC00 #999999 #FF0000   #FF6600
1  21   1 61debd3dea6d1aacce5c9fc7daec4fe5    <NA>    <NA>        <NA>      <NA>      <NA>      <NA>    <NA>    <NA>     Loved
2  21   1 b9e9309c0b1255a7efb2edf9ba66ae46    <NA>    Calm       Happy Energetic      <NA>      <NA>    <NA>    <NA>      <NA>
3  22   1 b17f9762462b37e7510f0e6d2534530d    <NA>  Lonely        <NA>      <NA>      <NA> Energetic    <NA>   Angry      <NA>
4  24   1 7bb50ca97a9b517239b39440a966d2f6 Excited    Calm        <NA>      <NA>      <NA>      <NA>   Empty    <NA> Noemotion
5  26   1 209f1ba8ef86e855deccc0aae120825c    <NA>    Calm Comfortable      <NA> Noemotion Noemotion    <NA>    <NA>      <NA>
    #FFCC00 #FFFFFF
1      <NA>   Empty
2      <NA>    <NA>
3      <NA>    Calm
4      <NA>    <NA>
5 Noemotion    <NA>

If I understand your objective correctly, reshape() is indeed the function you're looking for. Assuming your dataset is called mydf , try this:

reshape(mydf, direction = "wide", 
        idvar = c("hash", "age", "sex"), 
        timevar = "color")
#    age sex                             hash emotion.#006666 emotion.#66CC00
# 1   22   1 b17f9762462b37e7510f0e6d2534530d          Lonely       Energetic
# 5   24   1 7bb50ca97a9b517239b39440a966d2f6            Calm            <NA>
# 9   26   1 209f1ba8ef86e855deccc0aae120825c            Calm      No emotion
# 10  21   1 b9e9309c0b1255a7efb2edf9ba66ae46            Calm            <NA>
# 14  21   1 61debd3dea6d1aacce5c9fc7daec4fe5            <NA>            <NA>
# emotion.#FFFFFF emotion.#FF0000 emotion.#0033cc emotion.#999999 emotion.#FF6600
# 1             Calm           Angry            <NA>            <NA>            <NA>
# 5             <NA>            <NA>         Excited      Empty/void      No emotion
# 9             <NA>            <NA>            <NA>            <NA>            <NA>
# 10            <NA>            <NA>            <NA>            <NA>            <NA>
# 14      Empty/void            <NA>            <NA>            <NA>           Loved
# emotion.#330066 emotion.#330099 emotion.#FFCC00 emotion.#339900
# 1             <NA>            <NA>            <NA>            <NA>
# 5             <NA>            <NA>            <NA>            <NA>
# 9      Comfortable            <NA>      No emotion      No emotion
# 10           Happy       Energetic            <NA>            <NA>
# 14            <NA>            <NA>            <NA>            <NA>

You can rename the columns later if you need to.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM