Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 按列名和常量列名的向量筛选数据帧_R_Dataframe_Filter - Fatal编程技术网

R 按列名和常量列名的向量筛选数据帧

R 按列名和常量列名的向量筛选数据帧,r,dataframe,filter,R,Dataframe,Filter,这当然很容易,但就我个人而言,我找不到正确的语法 我想保留所有的“ID_”列,不管列数和附加的数字是多少,并按常量名称保留其他列 类似于下面的命令,该命令不起作用(每次对重新创建的数据): ####不起作用,但显示了我正在尝试做的事情 testdf1这是您想要的吗 library(tidyverse) df1 %>% select(matches("ID_*"), ConstantNames_YESwant) df1 %>% select(starts_with("I

这当然很容易,但就我个人而言,我找不到正确的语法

我想保留所有的“ID_”列,不管列数和附加的数字是多少,并按常量名称保留其他列

类似于下面的命令,该命令不起作用(每次对重新创建的数据):

####不起作用,但显示了我正在尝试做的事情
testdf1这是您想要的吗

library(tidyverse)

df1 %>% 
  select(matches("ID_*"), ConstantNames_YESwant)

df1 %>% 
  select(starts_with("ID"), ConstantNames_YESwant)

# ID_0 ID_1 ConstantNames_YESwant
# 1    0    1           wanted_data

在R基地,你可以

#Get all the ID columns
idvec <- grep("ID", colnames(df1), value = TRUE)
#Select ID columns and the constant names you want. 
df1[c(idvec, "ConstantNames_YESwant")]

#  ID_0 ID_1 ConstantNames_YESwant
#1    0    1           wanted_data
#获取所有ID列

idvec是什么?
idvec
?ID名称,我在玩grepl来剪切ID的'justid'是的,这就是我要找的。很好的dplyr管道,很好的呼叫。也可以只使用库(dplyr)。谢谢你!
library(tidyverse)

df1 %>% 
  select(matches("ID_*"), ConstantNames_YESwant)

df1 %>% 
  select(starts_with("ID"), ConstantNames_YESwant)

# ID_0 ID_1 ConstantNames_YESwant
# 1    0    1           wanted_data
#Get all the ID columns
idvec <- grep("ID", colnames(df1), value = TRUE)
#Select ID columns and the constant names you want. 
df1[c(idvec, "ConstantNames_YESwant")]

#  ID_0 ID_1 ConstantNames_YESwant
#1    0    1           wanted_data