Uploading Excel Data in R and Converting it to Code for Improved Management
Recently, I have uploaded a large excel dataset into R.
Grain= read_excel("Grain.xlsx", sheet=1)
Grain
This data consists of 7,849 columns. Upon checking the excel file size, it’s approximately 1MB in size. Now, I’d like to share this data with someone else. However, instead of attaching it as an excel file, I want to send it as R code, allowing them to work with the data directly in R. Therefore, all I need to do is convert this data into code.
Below is how you input the code:
dput(Grain)
When the data size is small, you can directly view the output as code in the console window.
structure(list(field = c("Spain", "Spain", "Spain", "Spain", "Spain", "Spain", "Spain", "Spain", "Spain", "Spain", "Spain", "Spain"), genotype = c("CV1", "CV1", "CV1", "CV1", "CV1", "CV1", "CV1", "CV1", "CV1", "CV1", "CV1", "CV1"), bock = c("I", "I", "I", "I", "I", "I", "I", "I", "I", "I", "I", "I"), treatment = c("Heat_stress", "Heat_stress", "Heat_stress", "Heat_stress", "Heat_stress", "Heat_stress", "Heat_stress", "Heat_stress", "Heat_stress", "Heat_stress", "Heat_stress", "Heat_stress"), length = c(6.9207737186671, 5.47034059643231, 6.8590945942035, 5.61411127338097, 6.26688587588373, 6.50942105582698, 6.36083481182545, 6.00131591951014, 6.23572828415701, 5.4865011651599, 5.43135905042144, 6.10661019591952), width = c(3.25186816873415, 2.67366942760534, 3.91616353007406, 2.53581205831367, 2.84241983369659, 2.17071374949856, 3.11499962994868, 2.33679557927825, 3.62898941127168, 2.71097704631433, 1.9169502530899, 3.18378822180417), area = c(16.6001994679937, 11.465204432563, 20.5222732622715, 11.2615753190867, 13.2093320566546, 10.6595414183812, 14.5904686524009, 10.2345763119956, 16.3877169148218, 11.5183250708542, 7.46344968081394, 14.6258824112774)), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, -12L))
I have successfully converted the data into R code. Now, you can save this code and whenever you need to share the data with someone, you can simply send them this code.
If you want to review the data after receiving this code, all you need to do is reassign the variables as shown below.
A= structure(list(field = c("Spain", "Spain", "Spain", "Spain", "Spain", "Spain", "Spain", "Spain", "Spain", "Spain", "Spain", "Spain"), genotype = c("CV1", "CV1", "CV1", "CV1", "CV1", "CV1", "CV1", "CV1", "CV1", "CV1", "CV1", "CV1"), bock = c("I", "I", "I", "I", "I", "I", "I", "I", "I", "I", "I", "I"), treatment = c("Heat_stress", "Heat_stress", "Heat_stress", "Heat_stress", "Heat_stress", "Heat_stress", "Heat_stress", "Heat_stress", "Heat_stress", "Heat_stress", "Heat_stress", "Heat_stress"), length = c(6.9207737186671, 5.47034059643231, 6.8590945942035, 5.61411127338097, 6.26688587588373, 6.50942105582698, 6.36083481182545, 6.00131591951014, 6.23572828415701, 5.4865011651599, 5.43135905042144, 6.10661019591952), width = c(3.25186816873415, 2.67366942760534, 3.91616353007406, 2.53581205831367, 2.84241983369659, 2.17071374949856, 3.11499962994868, 2.33679557927825, 3.62898941127168, 2.71097704631433, 1.9169502530899, 3.18378822180417), area = c(16.6001994679937, 11.465204432563, 20.5222732622715, 11.2615753190867, 13.2093320566546, 10.6595414183812, 14.5904686524009, 10.2345763119956, 16.3877169148218, 11.5183250708542, 7.46344968081394, 14.6258824112774)), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, -12L))
A
How about large-scale data?
However, in the case of large datasets, the R console may not display all the code at once. For instance, running dput(Grain)
might display the output starting from a certain point in the middle.
The structure of the data code should begin with structure(list()
. How can you resolve this issue in such cases? The solution is quite simple. You can just specify the filename like this:
dput(Grain, file="Grain_code")
This will automatically save the code file in the folder you designated when uploading the data.
Upon opening the file with a text editor, you can see that it’s saved as code. If necessary, you can copy and paste this code into the R script window. If the file is too large for copy-pasting, you can directly upload this file in R.
Now, you won’t have to deal with heavy excel files anymore. Instead, you can manage your files with such simple code.