读取R中的CSV并按名称过滤列 [英] Read CSV in R and filter columns by name
问题描述
比方说,我有一个包含数十或数百列的CSV,而我想仅提取2或3列.我了解中所述的colClasses
解决方案,这里,但是代码变得非常不可读.
Let's say I have a CSV with dozens or hundreds of columns and I want to pull in just about 2 or 3 columns. I know about the colClasses
solution as described here but the code gets very unreadable.
我想要来自 pandas的usecols
之类的东西'read_csv
.
I want something like usecols
from pandas' read_csv
.
加载所有内容并仅在事后选择不是解决方案(文件太大,无法容纳在内存中.)
Loading everything and just selecting afterwards is not a solution (the file is super big, it doesn't fit in memory).
推荐答案
我将使用包data.table
,然后使用fread()
指定要通过参数select
或drop
保留/删除的列.来自?fread
I will use package data.table
and then with fread()
specify columns to keep/drop by arguments select
or drop
. From ?fread
选择要保留的列名称或数字的向量,其余部分删除.
select Vector of column names or numbers to keep, drop the rest.
drop要删除的列名或数字的向量,其余部分保留.
drop Vector of column names or numbers to drop, keep the rest.
最好!
这篇关于读取R中的CSV并按名称过滤列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!