从文本文件中隔离R中的列

时间:2017-09-29 01:29:50

标签: r

我想分析保存在文本文件中的市场数据。 数据包括“日期时间;价格;大小”。我只想查看尺寸,如何在R中分离这些数据,以便我可以对尺寸进行统计分析?

示例:

20170918 040001;50.42;1
20170918 040002;50.42;1

3 个答案:

答案 0 :(得分:0)

只需使用分号作为分隔符的read.csv

df <- read.csv(file="path/to/your/file.csv", sep=";", header=TRUE)

可以使用df$Sizes访问尺寸。

答案 1 :(得分:0)

您可以使用select的{​​{1}}参数:

data.table

library(data.table) #[[1L]] extracts the column of the temporary table to a vector; # you could also use $V2, but this _may_ not be perfectly robust price = fread('/path/to/file'select = 2L)[[1L]] 应该能够自动检测到您的文件没有标题,以及字段分隔符为fread。如果没有,请设置;和/或header = FALSE

当然,您使用价格的向量不可能独立于其余数据。因此,您应该将整个数据文件存储在sep = ';'

data.table

然后你可以像market_data = fread('/path/to/file', col.names = c('date_time', 'price', 'size')) 那样操纵market_data(见Getting Started),例如

data.table

等等。

答案 2 :(得分:0)

df=read.table("your file")


size=df[4]

您的尺寸数据将作为数据框的大小