Question

I have this data.frame:

habitat <- data.frame(
    proportion = c(0.053, 0.139, 0.050, 0.756, 0.001, 0.084, 0.000, 0.011, 0.005, 0.066, 0.810, 0.025), 
    habitat = c("Non-irrigated arable land", "Pastures", "Natural grasslands", "Intertidal flats", "Sea and ocean", "Non-irrigated arable land", "Pastures", "Natural grasslands", "Beaches, dunes, sands", "Salt marshes", "Intertidal flats", "Sea and ocean"), 
    zone = c(rep("dark", 5), rep("light", 7)))

habitat

   proportion                   habitat  zone
1       0.053 Non-irrigated arable land  dark
2       0.139                  Pastures  dark
3       0.050        Natural grasslands  dark
4       0.756          Intertidal flats  dark
5       0.001             Sea and ocean  dark
6       0.084 Non-irrigated arable land light
7       0.000                  Pastures light
8       0.011        Natural grasslands light
9       0.005     Beaches, dunes, sands light
10      0.066              Salt marshes light
11      0.810          Intertidal flats light
12      0.025             Sea and ocean light

I need it reshaped to look like this:

enter image description here

Can this be done with reshape2?

Was it helpful?

Solution

Try using melt and then dcast

library(reshape2)
dcast(melt(habitat), ...~habitat)

Here's part of the output:

   zone   variable Beaches, dunes, sands Intertidal flats Natural grasslands
1  dark proportion                    NA            0.756              0.050
2 light proportion                 0.005            0.810              0.011

You can get rid of variable by using a simple subseting

dcast(melt(habitat), ...~habitat)[, -2]

OTHER TIPS

You can do this in the stats package (which is loaded by default when you start R) using xtabs....

xtabs(proportion ~zone + habitat , data = habitat )
#       habitat
#zone    Beaches, dunes, sands Intertidal flats Natural grasslands Non-irrigated arable land Pastures Salt marshes Sea and ocean
#  dark                  0.000            0.756              0.050                     0.053    0.139        0.000         0.001
#  light                 0.005            0.810              0.011                     0.084    0.000        0.066         0.025

Or to preserve the NAs you can use tapply() from the `base package...

tapply(habitat$proportion, list(habitat$zone, habitat$habitat) , sum , na.rm = FALSE )
#      Beaches, dunes, sands Intertidal flats Natural grasslands Non-irrigated arable land Pastures Salt marshes Sea and ocean
#dark                     NA            0.756              0.050                     0.053    0.139           NA         0.001
#light                 0.005            0.810              0.011                     0.084    0.000        0.066         0.025
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top