如何将 .csv 文件导入 R?

发布于 2024-09-12 08:40:02 字数 449 浏览 4 评论 0原文

我有这个 .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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

猫烠⑼条掵仅有一顆心 2024-09-19 08:40:02

您提到您将调用每个垂直列,以便可以执行计算。我假设您只想检查每个变量。这可以通过以下方式完成。

df <- read.csv("myRandomFile.csv", header=TRUE)

df$ID

df$GRADES

df$GPA

仅将数据分配给变量可能会有所帮助。

var3 <- df$GPA

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.

df <- read.csv("myRandomFile.csv", header=TRUE)

df$ID

df$GRADES

df$GPA

Might be helpful just to assign the data to a variable.

var3 <- df$GPA
只是我以为 2024-09-19 08:40:02

您需要 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.

愁杀 2024-09-19 08:40:02

正如德克所说,您需要的功能是“read.csv”或其他 read.table 变体之一。鉴于上面的示例数据,我认为您会想要执行以下操作:

setwd("c:/random/directory")

df <- read.csv("myRandomFile.csv", header=TRUE)

我们在上面所做的就是将目录设置为 .csv 文件所在的位置,然后将 .csv 读入名为 df 的数据帧。您可以通过检查对象的结构来检查数据是否正确加载:

str(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:

setwd("c:/random/directory")

df <- read.csv("myRandomFile.csv", header=TRUE)

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:

str(df)

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/

-残月青衣踏尘吟 2024-09-19 08:40:02

df <- read.csv("filename.csv", header=TRUE)

# To loop each column
for (i in 1:ncol(df))
    {
    dosomething(df[,i])
    }
# To loop each row
for (i in 1:nrow(df))
    {
    dosomething(df[i,])
    }

还可以使用apply函数(输入?applyhelp(apply))如果您愿意的话在每行/列上使用相同的函数

You can use

df <- read.csv("filename.csv", header=TRUE)

# To loop each column
for (i in 1:ncol(df))
    {
    dosomething(df[,i])
    }
# To loop each row
for (i in 1:nrow(df))
    {
    dosomething(df[i,])
    }

Also, you may want to have a look to the apply function (type ?apply or help(apply))if you want to use the same function on each row/column

π浅易 2024-09-19 08:40:02

请检查一下是否对您有帮助

df<-read.csv("F:/test.csv",header=FALSE,nrows=1)
df
V1 V2 V3 V4 V5
1 ID 等级 GPA 教师状态
a<-c(df)
一个[1]
$V1
[1] 身份证号
级别:ID

一个[2]
$V2
[1] 成绩
级别:等级

一个[3]
$V3
[1] 平均绩点
级别:GPA

一个[4]
$V4
[1] 老师
级别:教师

一个[5]
$V5
[1] 状态
级别:州

Please check this out if it helps you

df<-read.csv("F:/test.csv",header=FALSE,nrows=1)
df
V1 V2 V3 V4 V5
1 ID GRADES GPA Teacher State
a<-c(df)
a[1]
$V1
[1] ID
Levels: ID

a[2]
$V2
[1] GRADES
Levels: GRADES

a[3]
$V3
[1] GPA
Levels: GPA

a[4]
$V4
[1] Teacher
Levels: Teacher

a[5]
$V5
[1] State
Levels: State

弥枳 2024-09-19 08:40:02

既然你说你想在读入数据后按位置访问,你应该了解 R 的子集/索引函数。

最简单的是

df[row,column]
#example
df[1:5,] #rows 1:5, all columns
df[,5] #all rows, column 5. 

其他方法这里。我个人使用 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

df[row,column]
#example
df[1:5,] #rows 1:5, all columns
df[,5] #all rows, column 5. 

Other methods are here. I personally use the dplyr package for intuitive data manipulation (not by position).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文