如何将 .csv 文件导入 R?
我有这个 .csv 文件:
ID,GRADES,GPA,Teacher,State
3,"C",2,"Teacher3","MA"
1,"A",4,"Teacher1","California"
我想做的是使用 R 统计软件读取文件,并将标题读入某种列表或数组(我是 R 新手,一直在寻找如何做这个,但到目前为止还没有运气)。
这是我想要做的一些伪代码:
inputfile=read.csv("C:/somedirectory")
for eachitem in row1:{
add eachitem to list
}
然后我希望能够使用这些名称来调用每个垂直列,以便我可以执行计算。
我已经在谷歌上搜索了一个小时,试图找出如何做到这一点,但没有太多专门处理标头的信息。
感谢您的帮助!
I have this .csv file:
ID,GRADES,GPA,Teacher,State
3,"C",2,"Teacher3","MA"
1,"A",4,"Teacher1","California"
And what I want to do is read in the file using the R statistical software and read in the Header into some kind of list or array (I'm new to R and have been looking for how to do this, but so far have had no luck).
Here's some pseudocode of what I want to do:
inputfile=read.csv("C:/somedirectory")
for eachitem in row1:{
add eachitem to list
}
Then I want to be able to use those names to call on each vertical column so that I can perform calculations.
I've been scouring over google for an hour, trying to find out how to this but there is not much out there on dealing with headers specifically.
Thanks for your help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您提到您将调用每个垂直列,以便可以执行计算。我假设您只想检查每个变量。这可以通过以下方式完成。
仅将数据分配给变量可能会有所帮助。
You mention that you will call on each vertical column so that you can perform calculations. I assume that you just want to examine each single variable. This can be done through the following.
Might be helpful just to assign the data to a variable.
您需要 read.csv("C:/somedirectory/some/file.csv") ,一般来说,实际查看帮助页面(包括底部的示例部分)并没有什么坏处。
You need
read.csv("C:/somedirectory/some/file.csv")
and in general it doesn't hurt to actually look at the help page including its example section at the bottom.正如德克所说,您需要的功能是“read.csv”或其他 read.table 变体之一。鉴于上面的示例数据,我认为您会想要执行以下操作:
我们在上面所做的就是将目录设置为 .csv 文件所在的位置,然后将 .csv 读入名为 df 的数据帧。您可以通过检查对象的结构来检查数据是否正确加载:
假设数据正确加载,您可以继续对数据框中的数据执行任意数量的统计方法。我认为
summary(df)
将是一个很好的起点。学习如何使用 R 中的帮助将非常有用,快速阅读 CRAN 上的帮助将在将来为您节省大量时间:http://cran.r-project.org/As Dirk said, the function you are after is 'read.csv' or one of the other read.table variants. Given your sample data above, I think you will want to do something like this:
All we did in the above was set the directory to where your .csv file is and then read the .csv into a dataframe named df. You can check that the data loaded properly by checking the structure of the object with:
Assuming the data loaded properly, you can think go on to perform any number of statistical methods with the data in your data frame. I think
summary(df)
would be a good place to start. Learning how to use the help in R will be immensely useful, and a quick read through the help on CRAN will save you lots of time in the future: http://cran.r-project.org/您
还可以使用
apply
函数(输入?apply
或help(apply)
)如果您愿意的话在每行/列上使用相同的函数You can use
Also, you may want to have a look to the
apply
function (type?apply
orhelp(apply)
)if you want to use the same function on each row/column请检查一下是否对您有帮助
Please check this out if it helps you
既然你说你想在读入数据后按位置访问,你应该了解 R 的子集/索引函数。
最简单的是
其他方法这里。我个人使用 dplyr 包进行直观的数据操作(不是通过位置)。
Since you say you want to access by position once your data is read in, you should know about R's subsetting/ indexing functions.
The easiest is
Other methods are here. I personally use the dplyr package for intuitive data manipulation (not by position).