从R中的因子记录创建变量

时间:2015-03-18 09:46:49

标签: r dataframe lapply

我有点迷茫,我的数据框看起来像这样:

tract   ageClass    count
    1      [0-4]       71
    2      [0-4]      192
    3      [0-4]       81
    1      [5-8]        9
    2      [5-8]       86
    3      [5-8]       42

我想得到这样的结果:

tract   [0-4]   [5-8]
    1      71       9
    2     192      86
    3      81      42

我一直在网上寻找解决方案很长一段时间但到目前为止没有任何想法?

非常感谢!

2 个答案:

答案 0 :(得分:7)

我能想到的三种可能选项(假设您的数据集名为df

xtabs(count ~ tract + ageClass, df) 
#       ageClass
# tract [0-4] [5-8]
#     1    71     9
#     2   192    86
#     3    81    42

library(reshape2)
dcast(df, tract ~ ageClass, value.var = "count") 
#   tract [0-4] [5-8]
# 1     1    71     9
# 2     2   192    86
# 3     3    81    42

library(tidyr)
spread(df, ageClass, count)
#   tract [0-4] [5-8]
# 1     1    71     9
# 2     2   192    86
# 3     3    81    42

答案 1 :(得分:1)

ageClass <- c("[0-4]", "[5-8]")
ageClassDF <- lapply(ageClass, function(x) dx[which(dx$ageClass==x), ])

ageClassDF <- Reduce(function(...) merge(..., by = "tract.1", all = TRUE), ageClassDF )