如何收集用tidyr顺序编号的列

时间:2016-11-28 19:30:36

标签: r tidyr

我有一组列如下

rowid   TimePoint2   TimePoint3    TimePoint4     TimePoint5    TimePoint6

我想整理数据但是当我使用粘贴功能时,收集功能无法识别列名 我想要的是

rowid
1   TimePoint2    43
2   TimePoint3    34
3   TimePoint4    24
4   TimePoint5    22
5   TimePoint6    44

我的代码:

所以不要写:

y="TimePoint"
  Mydf<-
    select(df,matches(y),rowid)%>%
    gather(variable, value, TimePoint1,TimePoint2,TimePoint3,TimePoint4,TimePoint5,TimePoint6)

我想写一些类似的东西:

y="TimePoint"
  Mydf<-
    select(df,matches(y),rowid)%>%
    gather(variable, value, paste(y,1:10,",",sep=""))

上面提到的错误是Error: All select() inputs must resp;ve to integer column positions. The following do not: * paste (y,1:10,",",sep="")

2 个答案:

答案 0 :(得分:2)

我相信你可以做到:

Mydf <- select(df,matches(y),rowid)%>%
    gather(variable, value, starts_with("TimePoint"))

答案 1 :(得分:1)

由于您选择的是与“TimePoint”匹配的列,因此也可以使用:

library(dplyr)
library(tidyr)
y <- "TimePoint"
Mydf <- df %>% select(rowid,matches(y)) %>% 
               gather(variable,value,-1)
print(Mydf)
##  rowid   variable value
##1     1 TimePoint2    43
##2     1 TimePoint3    34
##3     1 TimePoint4    24
##4     1 TimePoint5    22
##5     1 TimePoint6    44

在这里,我们select同时将rowid列放在第一位。然后使用-1收集除第一列以外的所有列。

数据:

df <- structure(list(rowid = 1L, TimePoint2 = 43L, TimePoint3 = 34L, 
    TimePoint4 = 24L, TimePoint5 = 22L, TimePoint6 = 44L), .Names = c("rowid", 
"TimePoint2", "TimePoint3", "TimePoint4", "TimePoint5", "TimePoint6"
), class = "data.frame", row.names = c(NA, -1L))
##  rowid TimePoint2 TimePoint3 TimePoint4 TimePoint5 TimePoint6
##1     1         43         34         24         22         44