选择CSV文件的特定列包含R中的特定字母

发布于 2025-02-13 13:45:03 字数 156 浏览 1 评论 0原文

使用R读取CSV文件。

谁能与我分享如何仅选择以_CT结尾的列?

在此处输入图像描述

Using R to read a csv file.

Can anyone share with me how to select only columns end with _ct?

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

半城柳色半声笛 2025-02-20 13:45:03

使用readr :: read_csv()您可以指定要选择哪些列(在dplyr :: select()中使用tidySelection nes tidyselection nes select()。例如,如果我们要选择这些mtcars.csv数据以字母d开头,我们可以执行以下

library(readr)
library(dplyr)

read_csv(readr_example("mtcars.csv"), col_select = starts_with("d"))

#> Rows: 32 Columns: 2
#> ── Column specification ────────────────────────────────────────────────────────
#> Delimiter: ","
#> dbl (2): disp, drat
#> 
#> ℹ Use `spec()` to retrieve the full column specification for this data.
#> ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
#> # A tibble: 32 × 2
#>     disp  drat
#>    <dbl> <dbl>
#>  1  160   3.9 
#>  2  160   3.9 
#>  3  108   3.85
#>  4  258   3.08
#>  5  360   3.15
#>  6  225   2.76
#>  7  360   3.21
#>  8  147.  3.69
#>  9  141.  3.92
#> 10  168.  3.92
#> # … with 22 more rows

在2022-07-07创建的 reprex软件包(v2.0.1)

Using readr::read_csv() you can specify which columns to select (using tidyselection as we use in dplyr::select(). For example if we want to select those column in mtcars.csv data starts with letter d, we can do the following

library(readr)
library(dplyr)

read_csv(readr_example("mtcars.csv"), col_select = starts_with("d"))

#> Rows: 32 Columns: 2
#> ── Column specification ────────────────────────────────────────────────────────
#> Delimiter: ","
#> dbl (2): disp, drat
#> 
#> ℹ Use `spec()` to retrieve the full column specification for this data.
#> ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
#> # A tibble: 32 × 2
#>     disp  drat
#>    <dbl> <dbl>
#>  1  160   3.9 
#>  2  160   3.9 
#>  3  108   3.85
#>  4  258   3.08
#>  5  360   3.15
#>  6  225   2.76
#>  7  360   3.21
#>  8  147.  3.69
#>  9  141.  3.92
#> 10  168.  3.92
#> # … with 22 more rows

Created on 2022-07-07 by the reprex package (v2.0.1)

酒中人 2025-02-20 13:45:03

您可以使用 vroom软件包,例如

install.packages("vroom")
library(vroom)

data <- vroom("yourfile.csv", col_select = ends_with("_ct"))

You can specify which columns you want to read into R from a csv file using the vroom package, e.g.

install.packages("vroom")
library(vroom)

data <- vroom("yourfile.csv", col_select = ends_with("_ct"))
め七分饶幸 2025-02-20 13:45:03

对于基本R解决方案,我们可以在此处使用GREP

df_select <- df[, grep("_ct$", names(df), value=TRUE)]

For a base R solution, we can use grep here:

df_select <- df[, grep("_ct
quot;, names(df), value=TRUE)]
穿透光 2025-02-20 13:45:03

end_with dplyr软件包中的功能是您想要的。

 library(dplyr)
    
 df %>% dplyr::select(ends_with("_ct"))

The ends_with function in dplyr package is what you want.

 library(dplyr)
    
 df %>% dplyr::select(ends_with("_ct"))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文