我假设您已经使用 read.csv() 或 read.table() 函数在 R 中导入数据。(您可以通过 ? 直接在 R 中获得帮助,例如 ?read.csv
所以通常情况下,您有一个 data.frame。如果您检查documentation,data.frame 被描述为“[...]紧密耦合的变量集合,它们共享矩阵和列表的许多属性[...]”
所以基本上你已经可以将你的数据作为向量来处理了。
对 SO 的快速研究返回了这两个帖子:
而且我确信它们是更相关的。尝试一些关于 R 的优秀教程(在这种情况下,视频不是那么具有形成性)。
互联网上有很多好东西,例如:
* http://www.introductoryr.co.uk/R_Resources_for_Beginners.html(其中列出了一些)
或者
* http://tryr.codeschool.com/
无论如何,处理 csv 的一种方法是:
#import the data to R as a data.frame
mydata = read.csv(file="SomeFile.csv", header = TRUE, sep = ",",
quote = "\"",dec = ".", fill = TRUE, comment.char = "")
#extract a column to a vector
firstColumn = mydata$col1 # extract the column named "col1" of mydata to a vector
#This previous line is equivalent to:
firstColumn = mydata[,"col1"]
#extract a row to a vector
firstline = mydata[1,] #extract the first row of mydata to a vector
编辑:在某些情况下[1],您可能需要通过应用 as.numeric 或 as.character 等函数来强制转换向量中的数据:
firstline=as.numeric(mydata[1,])#extract the first row of mydata to a vector
#Note: the entire row *has to be* numeric or compatible with that class
[1] 例如当我想在嵌套函数中提取一行 data.frame 时发生在我身上