简体   繁体   中英

Tidy Data Layout - convert variables into factors

I have the following data table

| State | Prod. |Non-Prod.|
|-------|-------|---------|
|  CA   | 120   |   23    |
|  GA   | 123   |   34    |
|  TX   | 290   |   34    |

How can I convert this table to tiny data format in R or any other software like Excel?

|State | Class    | # of EEs|
|------|----------|---------|
| CA   | Prod.    | 120     |
| CA   | Non-Prod.| 23      |
| GA   | Prod.    | 123     |
| GA   | Non-Prod.| 34      |

Trying using reshape2 :

library(reshape2)
melt(df,id.vars='State')
#     State  variable value
# 1   CA         Prod   120
# 2   GA         Prod   123
# 3   TX         Prod   290
# 4   CA    Non-Prod.    23
# 5   GA    Non-Prod.    34
# 6   TX    Non-Prod.    34

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