select 如何在 R 中的名称中包含 UTF-8 符号的列?

How select columns with UTF-8 symbols in name in R?

我有 db 作为数据库,"Coś ktoś_był" 作为数据库中的列名。 我试过这个:

  temp_df <- db %>%
      select('Coś ktoś_był') 

但输出:

Error: Can't subset columns that don't exist.
x Column `Cos ktos_byl` doesn't exist.
Run `rlang::last_error()` to see where the error occurred.

我不知道如何在不更改列名的情况下使其正确。 我改不了!

试试这个:

library(dplyr)
temp_df <- db %>%
  select(matches("[^ -~]"))

或者,在 base R 中:

db[ , grepl("[^ -~]", names(db))]

这两种方法都将 select 任何 列名称中包含 non-ASCII 个字符。如果您需要更具体,您可以使用以下内容:

temp_df <- df %>%
  select(matches("^Co[^ -~]"))