简体   繁体   中英

R data.frame transformation?

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.

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!

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