How to Upload Data from GitHub Using R and Python?
I have soybean yield data that I want to upload to Github and access from R.
First, let’s upload the data to Github. The data should be in .csv
format. Click Add file
, choose Upload files
, and, after uploading, select the Raw
button to view the data in .csv
format as text.
and you can find the address for this data, starting with https://raw.githubusercontent.com/...
Let’s copy this address.
Next, I’ll bring this data into R from Github. Before that, let’s install the required package.
install.packages("readr")
library(readr)
Then, I’ll upload the data from Github to R. Paste the address you copied to the below code.
github="https://raw.githubusercontent.com/agronomy4future/raw_data_practice/main/soybean_yield.csv"
dataA=data.frame(read_csv(url(github), show_col_types=FALSE))
dataA
season crop plot treatment grain_yield
1 2023 Soybean 1 tr1 32.2
2 2023 Soybean 1 tr2 5.0
3 2023 Soybean 1 tr3 4.3
4 2023 Soybean 1 tr3 1.0
5 2023 Soybean 1 tr5 8.7
6 2023 Soybean 2 tr1 25.6
7 2023 Soybean 2 tr2 35.7
8 2023 Soybean 2 tr3 29.2
9 2023 Soybean 2 tr3 39.9
10 2023 Soybean 2 tr5 20.7
11 2023 Soybean 3 tr1 15.5
12 2023 Soybean 3 tr2 23.2
13 2023 Soybean 3 tr3 44.6
14 2023 Soybean 3 tr3 45.0
15 2023 Soybean 3 tr5 32.4
16 2023 Soybean 4 tr1 20.5
17 2023 Soybean 4 tr2 35.2
18 2023 Soybean 4 tr3 37.3
19 2023 Soybean 4 tr3 9.6
20 2023 Soybean 4 tr5 16.8
21 2023 Soybean 5 control 103.2
22 2023 Soybean 6 control 60.8
23 2023 Soybean 7 control 33.0
24 2023 Soybean 8 control 66.2
I have successfully imported the data from Github into R.
I’ll perform the same process using Python. First, let’s install the necessary package.
import pandas as pd
import requests
from io import StringIO
Then, let’s upload the data from Github.
github="https://raw.githubusercontent.com/agronomy4future/raw_data_practice/main/soybean_yield.csv"
response=requests.get(github)
dataA=pd.read_csv(StringIO(response.text))
dataA