Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/17.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_Regex - Fatal编程技术网

R 使用部分列名在数据框中选择多列

R 使用部分列名在数据框中选择多列,r,regex,R,Regex,我有一个数据框DF,列名称都包含颜色名称: red_balloons green_balloons red_balls blue_balls green_hats red_horses yellow_things 我想做的是基于多个颜色名称选择多个列。我可以一次选择一列,如下所示: grep("red", colnames(DF)) 但我无法理解或找到如何选择多个列。我试过这个: grep(c("red", "blue"), colnames(DF)) 但得到了以下警告: Warning

我有一个数据框DF,列名称都包含颜色名称:

red_balloons green_balloons red_balls blue_balls green_hats red_horses yellow_things
我想做的是基于多个颜色名称选择多个列。我可以一次选择一列,如下所示:

grep("red", colnames(DF))
但我无法理解或找到如何选择多个列。我试过这个:

grep(c("red", "blue"), colnames(DF))
但得到了以下警告:

Warning message:
In grep(c("mean()", "std()"), colnames(L21), value = FALSE) :
   argument 'pattern' has length > 1 and only the first element will be used
建议?

您可以在grep中使用“|”for”或“for”


成功了。你知道,我以为我已经试过了,但实际上我做的是grep(“red”|“blue”,DF,value=T)。谢谢。又短又甜,+1:)
grep("red|blue", DF, value=T)
# [1] "red_balloons" "red_balls"    "blue_balls"   "red_horses"