简体   繁体   中英

Reading CSV File into R as a Data Frame

I am trying to read into r as a data frame for the following link "https://github.com/rfordatascience/tidytuesday/blob/master/data/2020/2020-09-01/arable_land_pin.csv". I used both read.csv and read_csv from tidy verse but both do not work as one does not load and the other only has a single column (so it is not a formatting problem.) I am not sure how to proceed.

library(tidyverse)

arable_land <- read_csv("https://github.com/rfordatascience/tidytuesday/blob/master/data/2020/2020-09-01/arable_land_pin.csv")

arable_land %>% head

Try this:

#Code
df <- read.csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2020/2020-09-01/arable_land_pin.csv',
               stringsAsFactors = F,check.names = F)

Some output:

head(df)
       Entity Code Year Arable land needed to produce a fixed quantity of crops ((1.0 = 1961))
1 Afghanistan  AFG 1961                                                              1.0000000
2 Afghanistan  AFG 1962                                                              0.9842977
3 Afghanistan  AFG 1963                                                              1.0148702
4 Afghanistan  AFG 1964                                                              0.9391709
5 Afghanistan  AFG 1965                                                              0.9073304
6 Afghanistan  AFG 1966                                                              0.9268699

Or using readr :

#Code 2
df <- read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2020/2020-09-01/arable_land_pin.csv')

Output:

# A tibble: 11,280 x 4
   Entity      Code   Year `Arable land needed to produce a fixed quantity of crops ((1.0 = 1961))`
   <chr>       <chr> <dbl>                                                                    <dbl>
 1 Afghanistan AFG    1961                                                                    1    
 2 Afghanistan AFG    1962                                                                    0.984
 3 Afghanistan AFG    1963                                                                    1.01 
 4 Afghanistan AFG    1964                                                                    0.939
 5 Afghanistan AFG    1965                                                                    0.907
 6 Afghanistan AFG    1966                                                                    0.927
 7 Afghanistan AFG    1967                                                                    0.832
 8 Afghanistan AFG    1968                                                                    0.812
 9 Afghanistan AFG    1969                                                                    0.792
10 Afghanistan AFG    1970                                                                    0.876
# ... with 11,270 more rows

If you go to the GitHub page where the csv file if from, they give you the code to import the data. Try:

arable_land <- read.csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2020/2020-09-01/arable_land_pin.csv')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM