首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >重塑数据框-将行更改为列

重塑数据框-将行更改为列
EN

Stack Overflow用户
提问于 2012-07-31 00:48:18
回答 2查看 9.7K关注 0票数 5

假设我们有一个数据框,它看起来像

代码语言:javascript
复制
set.seed(7302012)

county         <- rep(letters[1:4], each=2)
state          <- rep(LETTERS[1], times=8)
industry       <- rep(c("construction", "manufacturing"), 4)
employment     <- round(rnorm(8, 100, 50), 0)
establishments <- round(rnorm(8, 20, 5), 0)

data <- data.frame(state, county, industry, employment, establishments)

  state county      industry employment establishments
1     A      a  construction        146             19
2     A      a manufacturing        110             20
3     A      b  construction        121             10
4     A      b manufacturing         90             27
5     A      c  construction        197             18
6     A      c manufacturing         73             29
7     A      d  construction         98             30
8     A      d manufacturing        102             19

我们希望重塑它,使每一行代表一个(州和)县,而不是一个县工业,具有列construction.employmentconstruction.establishments和类似的制造业版本。执行此操作的有效方法是什么?

一种方法是子集

代码语言:javascript
复制
construction <- data[data$industry == "construction", ]
names(construction)[4:5] <- c("construction.employment", "construction.establishments")

对于制造业也是如此,然后进行合并。如果只有两个行业,这并不是很糟糕,但是假设有14个行业;这个过程将变得单调乏味(尽管通过在industry级别上使用for循环可以减少这种情况)。

还有其他想法吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-07-31 00:59:17

如果我正确理解您的问题,这可以在base R重塑中完成:

代码语言:javascript
复制
reshape(data, direction="wide", idvar=c("state", "county"), timevar="industry")
#   state county employment.construction establishments.construction
# 1     A      a                     146                          19
# 3     A      b                     121                          10
# 5     A      c                     197                          18
# 7     A      d                      98                          30
#   employment.manufacturing establishments.manufacturing
# 1                      110                           20
# 3                       90                           27
# 5                       73                           29
# 7                      102                           19 
票数 8
EN

Stack Overflow用户

发布于 2012-07-31 01:03:14

还可以使用重塑软件包:

代码语言:javascript
复制
library(reshape) 
m <- reshape::melt(data) 
cast(m, state + county~...) 

让步:

代码语言:javascript
复制
> cast(m, state + county~...) 
  state county construction_employment construction_establishments manufacturing_employment manufacturing_establishments
1     A      a                     146                          19                      110                           20
2     A      b                     121                          10                       90                           27
3     A      c                     197                          18                       73                           29
4     A      d                      98                          30                      102                           19

我个人使用的是基本重塑,所以我可能应该使用reshape2 (Wickham)来展示这一点,但是忘记了有一个reshape2包。略有不同:

代码语言:javascript
复制
library(reshape2) 
m <- reshape2::melt(data) 
dcast(m, state + county~...) 
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11725964

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档