简体   繁体   English

峰值以获取R中CSV的标头值

[英]Peak to get header values of CSV in R

I would like to set colClasses programmatically based on text in the header, but first I need to peak into the csv to get the header names that will then be used to set colClasses . 我想基于标题中的文本以编程方式设置colClasses ,但是首先我需要深入研究csv以获得标题名称,然后将其用于设置colClasses How would I go about peaking? 我将如何达到峰值? Is there a way of doing this without loading the file? 有没有不用加载文件就可以做到这一点的方法吗?

You can use something along the lines: 您可以大致使用:

header <- read.table(csv.filename,sep=";",
                     nrows=3,header=T)

col.classes <- sapply(foo,class)
data <- as.data.frame(read.table(csv.filename,
                             sep=";",
                             header=T,
                             colClasses=col.classes))

or equivalent with read.csv 或等效于read.csv

header <- read.csv(csv.filename,sep=";",
                     nrows=3,header=T)

col.classes <- sapply(foo,class)
data <- read.csv(csv.filename,
                             sep=";",
                             header=T,
                             colClasses=col.classes)

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

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