Take the 2-minute tour ×

I have a R data frame that looks like this:

Country       Property   Value

Canada        Capital    Ottawa
Canada        Population 38
Canada        Language1  French
Canada        Language2  English
United States Capital    Washington 
United States Population 280
United States Language1  English
United States Language2  NA

I want to re-arrange this so that it looks like this:

Country         Capital     Population Language1 Language2

Canada          Ottawa      38         French    English
United States   Washington  280        English   NA

Is there any way to do this transformation ? Thanks.

share|improve this question

1 Answer 1

up vote 4 down vote accepted

As per Paul Hiemstra's comment:

the reshape2 package's dcast will do this nicely:

dcast(data=yourdataframe, Country~Property, value.var='Value')

If you've got duplicated values in there though it will try to aggregate them using length as a default, which isn't what you want!

share|improve this answer

Your Answer


