简体   繁体   English

如何在 R 中使用星号符号 select 变量

[英]How to select variables with star symbols in R

I want to select some variables from my csv file in R.我想 select R 文件中的 csv 文件中的一些变量。 I used this select(gender*, age*) , but got the error - object not found.我使用了这个select(gender*, age*) ,但得到了错误 - object not found。 I tried select(`gender*`, `age*`) and select(starts_with(gender), starts_with(age)) , but neither works.我尝试了select(`gender*`, `age*`)select(starts_with(gender), starts_with(age)) ,但都不起作用。 Does anyone know how to select variables with star symbols?有谁知道如何使用星号符号 select 变量? Thanks a lot!非常感谢!

It is possible that the select from dplyr is masked by select from any other package as this is working fine.来自dplyrselect可能被来自任何其他 ZEFE90A8E604A7C86BAD8B068Z 的select掩盖为正常工作。 Either specify the packagename with :: or do this on a fresh R session with only dplyr loaded使用::指定包名,或者在仅加载dplyr的新R session 上执行此操作

library(dplyr)
data(iris)
iris$'gender*' <- 'M'
iris%>% 
      head %>% 
      dplyr::select(`gender*`)
#   gender*
#1       M
#2       M
#3       M
#4       M
#5       M
#6       M

To select a list of column names starting with a specific string, one can use the starts_with() function in dplyr .对于 select 以特定字符串开头的列名列表,可以使用 dplyr 中的starts_with() dplyr To illustrate, we'll select the two columns that start with the string Sepal , as in Sepal.Length and Sepal.Width .为了说明,我们将 select 以字符串Sepal开头的两列,如Sepal.LengthSepal.Width

library(dplyr)
select(iris,starts_with("Sepal")) %>% head()

...and the output: ...和 output:

> select(iris,starts_with("Sepal")) %>% head()
  Sepal.Length Sepal.Width
1          5.1         3.5
2          4.9         3.0
3          4.7         3.2
4          4.6         3.1
5          5.0         3.6
6          5.4         3.9
>

We can do the same thing in Base R with grepl() and a regular expression.我们可以使用grepl()和正则表达式在 Base R 中做同样的事情。

# base R version
head(iris[,grepl("^Sepal",names(iris))])

...and the output: ...和 output:

> head(iris[,grepl("^Sepal",names(iris))])
  Sepal.Length Sepal.Width
1          5.1         3.5
2          4.9         3.0
3          4.7         3.2
4          4.6         3.1
5          5.0         3.6
6          5.4         3.9
>

Also note that if one is using read.csv() to create a data frame in R, it converts any occurrences of * in column headings to .另请注意,如果使用read.csv()在 R 中创建数据框,它会将列标题中出现的任何*转换为. . .

# confirm that * is converted to . in read.csv()
textFile <- 'v*1,v*2
1,2
3,4
5,6'
data <- read.csv(text = textFile,header = TRUE)
# see how illegal column name * is converted to . 
names(data)

...and the output: ...和 output:

> names(data)
[1] "v.1" "v.2"
> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM