【问题标题】:Reshape long to wide where most columns have multiple values在大多数列具有多个值的情况下,将长整形为宽
【发布时间】:2017-06-18 08:03:26
【问题描述】:

我的数据如下:

IDnum   zipcode   City          County   State
10011   36006     Billingsley   Autauga  AL 
10011   36022     Deatsville    Autauga  AL
10011   36051     Marbury       Autauga  AL
10011   36051     Prattville    Autauga  AL
10011   36066     Prattville    Autauga  AL
10011   36067     Verbena       Autauga  AL
10011   36091     Selma         Autauga  AL
10011   36703     Jones         Autauga  AL
10011   36749     Plantersville Autauga  AL
10011   36758     Uriah         Autauga  AL
10011   36480     Atmore        Autauga  AL
10011   36502     Bon Secour    Autauga  AL

我有一个邮政编码列表、它们所包含的城市以及它们所在的县/州。IDnum = 县和州的数字值,组合在一起。列表是你现在看到的格式,我需要将它从长到宽/垂直到水平重塑,其中 IDnum 变量成为唯一标识符,所有其他可能的值组合成为宽变量。

IDnum  zip1   city1        county1  state1  zip2   city2       county2
10011  36006  Billingsley  Autauga  AL      36022  Deatsville  Autauga

这只是数据集的示例,它包含美国的每个 zip 并包含更多变量。我见过与此类似的其他问题和答案,但不是几乎每一列都有多个值。

SPSS 和 STATA 中有一些命令可以通过这种方式重塑数据,在 SPSS 中,我可以运行 Restructure/Cases to Vars 命令,将我的初始数据集中的 11 个变量变成大约 1750 个变量,b/c 一个县有超过 290 个拉链它复制了大多数其他变量 290 多次。这将创建许多空白,但我需要将其重新塑造成一个非常长的水平文件。

我查看了 reshape 和 reshape2,并被“默认长度”错误消息挂断。我确实得到了melt/dcast来排序工作,但这会创建一个变量,它是所有值的列表,而不是为每个值创建变量。

melted_dupes <- melt(zip_code_list_dupes, id.vars= c("IDnum"))
HRZ_dupes <- dcast(melted_dupes, IDnum ~ variable, fun.aggregate = list) 

我尝试过 tidyr 和 dplyr,但在语法上迷路了。有点惊讶,在其他包中没有类似于内置命令的命令数据,让我假设有,但我还没弄明白。

感谢任何帮助。

【问题讨论】:

    标签: r reshape reshape2 data-management


    【解决方案1】:

    可能有更有效的方法,但请尝试以下方法。 我使用了我自己的(示例)数据集,与您的非常相似。 逐步运行该过程以查看其工作原理,因为您必须修改代码中的一些内容。

    library(dplyr)
    library(tidyr)
    
    # get example data
    dt = data.frame(id = c(1,1,1,2,2),
    zipcode = c(4,5,6,7,8),
    city = c("A","B","C","A","C"),
    county = c("A","B","C","A","C"),
    state = c("A","B","C","A","C"))
    
    dt
    
    #   id zipcode city county state
    # 1  1       4    A      A     A
    # 2  1       5    B      B     B
    # 3  1       6    C      C     C
    # 4  2       7    A      A     A
    # 5  2       8    C      C     C
    
    
    # get maximum number of rows for a single id
    # this will help you get the wide format
    max_num_rows = max((dt %>% count(id))$n)
    
    # get names of columns to reshape
    col_names = names(dt)[-1]
    
    dt %>% 
    group_by(id) %>% 
    mutate(nrow = paste0("row",row_number())) %>%
    unite_("V",col_names) %>%
    spread(nrow, V) %>%
    unite("z",matches("row")) %>%
    separate(z, paste0(col_names, sort(rep(1:max_num_rows, ncol(dt)-1))), convert=T) %>%
    ungroup()
    
    # # A tibble: 2 × 13
    #      id zipcode1 city1 county1 state1 zipcode2 city2 county2 state2 zipcode3 city3 county3 state3
    # * <dbl>    <int> <chr>   <chr>  <chr>    <int> <chr>   <chr>  <chr>    <int> <chr>   <chr>  <chr>
    # 1     1        4     A       A      A        5     B       B      B        6     C       C      C
    # 2     2        7     A       A      A        8     C       C      C       NA  <NA>    <NA>   <NA>
    

    【讨论】:

      【解决方案2】:

      您可以在添加IDnum 的连续计数后使用基本函数reshape 执行此操作。假设您的数据存储在名为dfdata.frame 中:

      df2 <- within(df, count <- ave(rep(1,nrow(df)),df$IDnum,FUN=cumsum)) 
      

      提供一个名为“时间”的连续计数的新列。现在我们可以reshape 转换为宽格式

      reshape(df2,direction="wide",idvar="IDnum",timevar="count") 
      
      IDnum zipcode.1 City.1 County.1 State.1 zipcode.2 City.2 County.2 State.2 zipcode.3 City.3 County.3 State.3 zipcode.4 City.4 County.4 State.4 1 10011 36006 Billingsley Autauga AL 36022 Deatsville Autauga AL 36051 Marbury Autauga AL 36051 Prattville Autauga AL

      (输出被截断,一直到 zipcode.12 等)

      【讨论】:

      • 谢谢!如何让它显示在数据框中?我正在使用 RStudio,结果显示了我希望看到的 varnames,但它都在控制台中,我查看了 DataPane,它仍然显示 43,xxxx obs of 12 个变量,而不是 3,xxx obs 1750 个变量。我错过了什么吗?如何让结果实际显示在数据框中,以便我可以查看它,而不仅仅是在控制台中?我还想创建一个顺序计数变量,所以也谢谢你。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-01-15
      • 1970-01-01
      • 1970-01-01
      • 2016-05-02
      • 1970-01-01
      • 1970-01-01
      • 2018-06-24
      相关资源
      最近更新 更多