【问题标题】:reshape multi id repeated variable readings from long to wide从长到宽重塑多 id 重复变量读数
【发布时间】:2018-02-17 17:49:20
【问题描述】:

这就是我所拥有的:

id<-c(1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2)
measure<-c("speed","weight","time","speed","weight","time","speed","weight","time",
           "speed","weight","time","speed","weight","time","speed","weight","time")
value<-c(1.23,10.3,33,1.44,10.4,31,1.21,10.1,33,4.25,12.5,38,1.74,10.8,31,3.21,10.3,33)
testdf<-data.frame(id,measure,value) 

这就是我想要的:

id<-c(1,1,1,2,2,2)  
speed<-c(1.23,1.44,1.21,4.25,1.74,3.21)
weight<-c(10.3,10.4,10.1,12.5,10.8,10.3)
time<-c(33,31,33,37,31,33)
res<-data.frame(id,speed,weight,time) 

问题在于我的变量速度重量和时间重复。我可以使用带有 if 语句的 for 循环来完成它,但它是一个令人头疼的问题,而且效率不高。这是我在 stackoverflow 上的第一篇文章……长时间的用户第一次提问……谢谢你们!

【问题讨论】:

    标签: r reshape data-science reshape2


    【解决方案1】:

    使用来自 data.table 的rowid(非常类似于@Kelli-Jean 的回答):

    library(reshape2)
    
    testdf$r <- data.table::rowid(testdf$measure); 
    dcast(testdf, id + r ~ measure)
    
      id r speed time weight
    1  1 1  1.23   33   10.3
    2  1 2  1.44   31   10.4
    3  1 3  1.21   33   10.1
    4  2 4  4.25   38   12.5
    5  2 5  1.74   31   10.8
    6  2 6  3.21   33   10.3
    

    或者在一行中dcast(testdf, id + data.table::rowid(measure) ~ measure)

    或者没有data.table,添加testdf$r &lt;- ave(testdf$id, testdf$meas, FUN = seq_along)

    或者,如果您准备学习 data.table 包:

    library(data.table)
    setDT(testdf)
    testdf[, r := rowid(measure)]
    dcast(testdf, id + r ~ measure)
    

    【讨论】:

    • 我对之前发布的答案感到困惑,但是你的答案很有意义!
    • ~添加了我的 :),感谢您的出色回答,澄清这个问题
    • 你为什么使用reshape2?您是否尝试将其保存为 data.frame 格式?
    • 为什么不发布完整的data.table 解决方案:library(data.table); dcast(as.data.table(testdf)[, rn := rowid(measure)], id + rn ~ measure)[, -"rn"]dcast(setDT(testdf), paste(id, rowid(measure), sep = "_") ~ measure)
    【解决方案2】:

    如果你想走 tidyverse 路线:

    library(tidyr)
    library(dplyr)
    testdf %>% 
      # add unique id for rows to be able to use spread
      group_by(measure) %>% mutate(unique_id = row_number()) %>% 
      spread(measure, value) %>% select(-unique_id )
    

    R Cookbook 是解决这类问题的绝佳资源:http://www.cookbook-r.com/Manipulating_data/Converting_data_between_wide_and_long_format/

    【讨论】:

      【解决方案3】:

      这是我的解决方案

      library(plyr)
      
      a=daply(testdf, .(id, measure), function(x) x$value)
      listdf=apply(a, c(3), function(x) rbind(data.frame(x,id=row.names(x))))
      df <- ldply(listdf, data.frame)
      df$.id=NULL
      df <- df[order(df$id),] 
      df
      
        speed time weight id
      1  1.23   33   10.3  1
      3  1.44   31   10.4  1
      5  1.21   33   10.1  1
      2  4.25   38   12.5  2
      4  1.74   31   10.8  2
      6  3.21   33   10.3  2
      

      【讨论】:

        【解决方案4】:

        安装 reshape2 以帮助首先重新格式化数据

        然后创建另一个标识符,以帮助一次按三个连续行组织数据,就像您在所需数据集中一样。

        A<-c("A","A","A")
        B<-c("B","B","B")
        C<-c("C","C","C")
        D<-c("D","D","D")
        E<-c("E","E","E")
        F<-c("F","F","F")
        
        A <- as.data.frame(A)
        colnames(A) <- "id2"
        B <- as.data.frame(B)
        colnames(B) <- "id2"
        C <- as.data.frame(C)
        colnames(C) <- "id2"
        D <- as.data.frame(D)
        colnames(D) <- "id2"
        E <- as.data.frame(E)
        colnames(E) <- "id2"
        F <- as.data.frame(F)
        colnames(F) <- "id2"
        

        将单独的数据集逐行绑定在一起

        x<-rbind(A,B,C,D,E,F)
        

        将此新标识符绑定到 testdf 列

        testdf <- cbind(testdf, x)
        

        将数据从长格式转换为宽格式

        x2<-dcast(testdf, id + id2 ~ measure, value.var="value")
        

        这是生成的数据集:

          id id2 speed time weight
        1  1   A  1.23   33   10.3
        2  1   B  1.44   31   10.4
        3  1   C  1.21   33   10.1
        4  2   D  4.25   38   12.5
        5  2   E  1.74   31   10.8
        6  2   F  3.21   33   10.3
        

        如果需要,您可以删除 id2 变量

         testdf$id2 <- NULL
        

        【讨论】:

          猜你喜欢
          • 2018-05-05
          • 1970-01-01
          • 2012-12-01
          • 2017-10-13
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-11-22
          相关资源
          最近更新 更多