How to get a .csv file into R?

后端 未结 6 396
名媛妹妹
名媛妹妹 2020-11-30 07:27

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

6条回答
  •  一向
    一向 (楼主)
    2020-11-30 07:42

    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/

提交回复
热议问题