使用dcast或ftable重塑R中的数据帧

时间:2019-12-05 07:38:53

标签: r

我目前有一个看起来像这样的数据框。

country2<-c("Afghanistan","Afghanistan","Afghanistan")
continent2<-c("Asia","Asia","Asia")
series<-c('lifeexp','pop','gdp')
y1901<-c('1','3','100')
y1902<-c('2','4','101')
y1903<-c('2','4','101')
y1904<-c('2','4','101')
y1905<-c('2','4','101')
y1906<-c('2','4','101')
y1907<-c('2','4','101')

df<-data.frame(country2,continent2,series,y1901,y1902,y1903,y1904,y1905,y1906,y1907)


     country2 continent2  series y1901 y1902 y1903 y1904 y1905 y1906 y1907
1 Afghanistan       Asia lifeexp     1     2     2     2     2     2     2
2 Afghanistan       Asia     pop     3     4     4     4     4     4     4
3 Afghanistan       Asia     gdp   100   101   101   101   101   101   101

如何重塑这些数据,使其看起来像这样?

country<-c("Afghanistan","Afghanistan","Afghanistan","Afghanistan","Afghanistan","Afghanistan","Afghanistan")
continent<-c("Asia","Asia","Asia","Asia","Asia","Asia","Asia")
year<-c("1901","1902","1903","1904","1905","1906","1907")
lifeexp<-c("1","2","2","2","2","2","2")
pop<-c('3','4','4','4','4','4','4')
gdp<-c('100','101','101','101','101','101','101')

df<-data.frame(country,continent,year,lifeexp,pop,gdp)

      country continent year lifeexp pop gdp
1 Afghanistan      Asia 1901       1   3 100
2 Afghanistan      Asia 1902       2   4 101
3 Afghanistan      Asia 1903       2   4 101
4 Afghanistan      Asia 1904       2   4 101
5 Afghanistan      Asia 1905       2   4 101
6 Afghanistan      Asia 1906       2   4 101
7 Afghanistan      Asia 1907       2   4 101

我尝试使用reshape2中的dcast2来重塑数据,但我只能为value.var输入1列。

dcast(df,country+region~series,value.var ='y1901',fun.aggregate = sum)

我也尝试使用ftable和xtabs,但是我仍然不确定如何为该值输入多于1列。下面的代码给出了错误。

ftable(xtabs(c(y2000,y2001)~country+region+series,df))

谢谢

2 个答案:

答案 0 :(得分:0)

我知道您正在寻找使用ftabledcast的解决方案,但就您所知,您可以使用tidyr来实现:

library(tidyverse)
df %>% 
  pivot_longer(., cols = starts_with("y190"), names_to = "year", values_to = "Value") %>%
  pivot_wider(., names_from = "series", values_from = "Value")  %>%
  mutate(year = gsub("y","", year)) %>% 
  rename(country = country2, continent = continent2)

# A tibble: 7 x 6
  country     continent year  lifeexp pop   gdp  
  <fct>       <fct>     <chr> <fct>   <fct> <fct>
1 Afghanistan Asia      1901  1       3     100  
2 Afghanistan Asia      1902  2       4     101  
3 Afghanistan Asia      1903  2       4     101  
4 Afghanistan Asia      1904  2       4     101  
5 Afghanistan Asia      1905  2       4     101  
6 Afghanistan Asia      1906  2       4     101  
7 Afghanistan Asia      1907  2       4     101  

答案 1 :(得分:0)

使用data.tablemelt的{​​{1}}方法可能是

dcast