在 R 中以特定顺序提取或合并列

extracting or merging columns in a specific order in R

我有一个包含 300 多个变量的纵向数据框,一个医院数据库。对于某个临床测试,我提取了该测试的测试值和访问日期,这也对应于以下带有 dplyr 包的测试日期:

df_VL<- select(df, ends_with("vload"))
df_dat<- select(df, ends_with("datvisit"))

然后我 merge 这两个 cbind:

df_x<- cbind(df_VL,df_dat)

但这首先给了我所有的测试结果,然后是测试日期。

我也需要 按时间顺序逐一提取所有以 "vload""datvisit" 结尾的变量 --> "t0datvisit", "t0vload","t6datvisit", "t6vload",......."t180datvisit","t180vload" 要么 根据这个顺序再次合并两个数据框的列 --> "t0datvisit", "t0vload","t6datvisit", "t6vload",......."t180datvisit", "t180vload"

知道怎么做吗?

我认为这可行

colnames( mtcars )[1:6]<- c( "t0datvisit", "t0vload","t6datvisit", "t6vload","t180datvisit", "t180vload")

# get all the numbers out of the colnames   
matches <- regmatches(colnames(mtcars), gregexpr("[[:digit:]]+",  colnames( mtcars)))
a<-unique( as.numeric(unlist(matches)) )
#order them numerically
a <- sort(a )

# create an object with the ars ordered numerically
f <- NULL 
for( b in a){ 
f <- c( f ,  paste0("t" , b, "datvisit") ,  paste0("t" , b, "vload")  )
}

# just those vars 
head( mtcars[ , f ] )

# or those vars and the other cols
others <- colnames( mtcars )[ !(colnames(mtcars) %in% f) ]
head( mtcars[ ,c( others, f)  ] )

如果您想完成所有 "datvisits" 并且比 "vloads" 容易得多

head( mtcars[ , c( 
            grep(  "datvisit" , colnames( mtcars) ) ,
            grep(  "vload" , colnames( mtcars) )
            )])

考虑 mapply 映射两个数据框名称,然后转换为新列名称顺序的字符向量:

df_x <- cbind(df_VL, df_dat)

ord_names <- as.vector(mapply(c, names(df_VL), names(df_dat)))

df_x <- df_x[ord_names]