如何根据以下格式制作索引和拆分列

时间:2018-06-12 06:01:52

标签: r dataframe

如何根据以下格式制作索引和拆分列

数据帧:DF

 ID    v1         v2       v3     v4    v5            v6     v7        v8     v9    v10      v11       v12        v13     v14   v15
   1    adi     20180501   4      US   california    john   20180504   4      AP   Vizag     ram     20180503      5      MP   Bhopal
   2    surya   20180502   5      MP   Bhopal        vamsi  20180505   5      UP   Lucknow   vijay   20180503      5      MP   Bhopal
   3    ram     20180503   5      MP   Bhopal        sam    20180503   5      MP   Bhopal    ram     20180503      5      MP   Bhopal

所需的数据帧:DF1

ID       v1         v2       v3     v4    v5          
   1    adi     20180501     4      US   california
       john     20180504     4      AP   Vizag 
       ram      20180503     5      MP   Bhopal
   2   surya    20180502     5      MP   Bhopal
       vamsi    20180505     5      UP   Lucknow   
       vijay    20180503     5      MP   Bhopal
   3   ram      20180503     5      MP   Bhopal 
       sam      20180503     5      MP   Bhopal 
       ram      20180503     5      MP   Bhopal 

任何想法如何获得所需的格式DF2,尝试不同的循环,没有运气。

2 个答案:

答案 0 :(得分:0)

我们可以设计一个处理数据框的函数,使用mapply重复该过程,然后使用do.callrbind生成最终结果。

dat_list <- mapply(function(x, i, n = 5){
  x2 <- x[, c("ID", paste0("v", (1:n) + n * (i - 1)))]
  names(x2) <- c("ID", paste0("v", (1:n)))
  x2[["ID"]] <- i
  return(x2)
}, i = 1:3, MoreArgs = list(x = dat), SIMPLIFY = FALSE)

dat2 <- do.call(rbind, dat_list)

dat2
#   ID    v1       v2 v3 v4         v5
# 1  1   adi 20180501  4 US california
# 2  1 surya 20180502  5 MP     Bhopal
# 3  1   ram 20180503  5 MP     Bhopal
# 4  2  john 20180504  4 AP      Vizag
# 5  2 vamsi 20180505  5 UP    Lucknow
# 6  2   sam 20180503  5 MP     Bhopal
# 7  3   ram 20180503  5 MP     Bhopal
# 8  3 vijay 20180503  5 MP     Bhopal
# 9  3   ram 20180503  5 MP     Bhopal

数据

dat <- read.table(text = "ID    v1         v2       v3     v4    v5            v6     v7        v8     v9    v10      v11       v12        v13     v14   v15
   1    adi     20180501   4      US   california    john   20180504   4      AP   Vizag     ram     20180503      5      MP   Bhopal
                  2    surya   20180502   5      MP   Bhopal        vamsi  20180505   5      UP   Lucknow   vijay   20180503      5      MP   Bhopal
                  3    ram     20180503   5      MP   Bhopal        sam    20180503   5      MP   Bhopal    ram     20180503      5      MP   Bhopal",
                  header = TRUE, stringsAsFactors = FALSE)

答案 1 :(得分:0)

您可以使用基础r中的reshape

reshape(dat,matrix(2:ncol(dat),5),dir="long")

    ID time    v1       v2 v3 v4         v5 id
1.1  1    1   adi 20180501  4 US california  1
2.1  2    1 surya 20180502  5 MP     Bhopal  2
3.1  3    1   ram 20180503  5 MP     Bhopal  3
1.2  1    2  john 20180504  4 AP      Vizag  1
2.2  2    2 vamsi 20180505  5 UP    Lucknow  2
3.2  3    2   sam 20180503  5 MP     Bhopal  3
1.3  1    3   ram 20180503  5 MP     Bhopal  1
2.3  2    3 vijay 20180503  5 MP     Bhopal  2
3.3  3    3   ram 20180503  5 MP     Bhopal  3

或者您可以使用data.table

library(data.table)
melt(setDT(dat), "ID",data.frame(t(matrix(2:ncol(dat),5))))

   ID variable value1   value2 value3 value4     value5
1:  1        1    adi 20180501      4     US california
2:  2        1  surya 20180502      5     MP     Bhopal
3:  3        1    ram 20180503      5     MP     Bhopal
4:  1        2   john 20180504      4     AP      Vizag
5:  2        2  vamsi 20180505      5     UP    Lucknow
6:  3        2    sam 20180503      5     MP     Bhopal
7:  1        3    ram 20180503      5     MP     Bhopal
8:  2        3  vijay 20180503      5     MP     Bhopal
9:  3        3    ram 20180503      5     MP     Bhopal

如果您需要排列行,您将执行以下操作:

melt(setDT(dat),id.vars = "ID",data.frame(t(matrix(2:ncol(dat),5))))[order(ID)][,variable:=NULL][]
   ID value1   value2 value3 value4     value5
1:  1    adi 20180501      4     US california
2:  1   john 20180504      4     AP      Vizag
3:  1    ram 20180503      5     MP     Bhopal
4:  2  surya 20180502      5     MP     Bhopal
5:  2  vamsi 20180505      5     UP    Lucknow
6:  2  vijay 20180503      5     MP     Bhopal
7:  3    ram 20180503      5     MP     Bhopal
8:  3    sam 20180503      5     MP     Bhopal
9:  3    ram 20180503      5     MP     Bhopal