dataframe - How to directly open .R data containing data frame code in R? - Stack Overflow

admin2025-04-19  0

Here is an dataset

Environment= c("ENV1","ENV2","ENV3","ENV4","ENV5")
CV1<- c(25.2,26.3,15.2,22.1,21.9)
CV2<- c(37.1,34.5,26.0,29.9,28.3)
CV3<- c(33.0,30.5,25.0,30.2,28.9)
CV4<- c(35.4,37.0,28.0,26.0,34.0)
CV5<- c(34.6,32.7,20.0,32.3,28.6)

dataA= data.frame(Environment,CV1,CV2,CV3,CV4,CV5)

and I want to convert this data table as data frame code

dput(dataA)

structure(list(Environment = c("ENV1", "ENV2", "ENV3", "ENV4", 
                               "ENV5"), CV1 = c(25.2, 26.3, 15.2, 22.1, 21.9), 
               CV2 = c(37.1, 34.5, 26, 29.9, 28.3), 
               CV3 = c(33, 30.5, 25, 30.2, 28.9), 
               CV4 = c(35.4, 37, 28, 26, 34), 
               CV5 = c(34.6, 32.7, 20, 32.3, 28.6)), 
              class = "data.frame", row.names = c(NA, -5L))

Then, I saved this code as .R data for some cases. I saved as data_code.R in my PC.

Later, I want to open this data again, but directly want to open it as data table. For example, I want like

df= C:/Users/Desktop/data_code.R

print(df)
  Environment  CV1  CV2  CV3  CV4  CV5
1        ENV1 25.2 37.1 33.0 35.4 34.6
2        ENV2 26.3 34.5 30.5 37.0 32.7
3        ENV3 15.2 26.0 25.0 28.0 20.0
4        ENV4 22.1 29.9 30.2 26.0 32.3
5        ENV5 21.9 28.3 28.9 34.0 28.6

Is that possible to import data frame code to directly R?

Thanks,

Here is an dataset

Environment= c("ENV1","ENV2","ENV3","ENV4","ENV5")
CV1<- c(25.2,26.3,15.2,22.1,21.9)
CV2<- c(37.1,34.5,26.0,29.9,28.3)
CV3<- c(33.0,30.5,25.0,30.2,28.9)
CV4<- c(35.4,37.0,28.0,26.0,34.0)
CV5<- c(34.6,32.7,20.0,32.3,28.6)

dataA= data.frame(Environment,CV1,CV2,CV3,CV4,CV5)

and I want to convert this data table as data frame code

dput(dataA)

structure(list(Environment = c("ENV1", "ENV2", "ENV3", "ENV4", 
                               "ENV5"), CV1 = c(25.2, 26.3, 15.2, 22.1, 21.9), 
               CV2 = c(37.1, 34.5, 26, 29.9, 28.3), 
               CV3 = c(33, 30.5, 25, 30.2, 28.9), 
               CV4 = c(35.4, 37, 28, 26, 34), 
               CV5 = c(34.6, 32.7, 20, 32.3, 28.6)), 
              class = "data.frame", row.names = c(NA, -5L))

Then, I saved this code as .R data for some cases. I saved as data_code.R in my PC.

Later, I want to open this data again, but directly want to open it as data table. For example, I want like

df= C:/Users/Desktop/data_code.R

print(df)
  Environment  CV1  CV2  CV3  CV4  CV5
1        ENV1 25.2 37.1 33.0 35.4 34.6
2        ENV2 26.3 34.5 30.5 37.0 32.7
3        ENV3 15.2 26.0 25.0 28.0 20.0
4        ENV4 22.1 29.9 30.2 26.0 32.3
5        ENV5 21.9 28.3 28.9 34.0 28.6

Is that possible to import data frame code to directly R?

Thanks,

Share asked Mar 5 at 18:44 J.K KimJ.K Kim 9442 gold badges12 silver badges24 bronze badges 6
  • Try saving it as .RDS or .RData and then load it if needed see this. Or just save as .txt/.csv/.csv2 and read. There are a lot of ways to do this in R. – Tim G Commented Mar 5 at 18:59
  • 1 @TimG Thank you for your comment. Sometimes I lose my data in my PC and couldn't upload to R. So, I want to save it as data.frame code so that I can directly use data. But sometimes, the code is too long to output in the R script. So, I want to save it as .R data and import from them. This is because of losing excel/csv data as time goes by. – J.K Kim Commented Mar 5 at 19:28
  • Ahh so that's what I thought. You want to store the dput-text inside an R-Script file and then load this script file and parse it. Still I am wondering where the advantage is of storing the defining Code for a data.frame opposed to storing and restoring the data directly. Unless you create the data.frame using some sort of sampling/ rep that is depending on a seed. – Tim G Commented Mar 5 at 19:36
  • I think this question has dozens, if not hundreds, of answers. Each text book about R has an I/O section. – Friede Commented Mar 5 at 19:50
  • @TimG Yes, it seems less convenient, but I used to lose my excel files, and without data, I cannot do anything. So, first, I planned to save data.frame code directly in my R script so that when I open R file, I can easily use it. However, sometimes I have 100 MB excel file, and when I convert it to data.frame code using dput(), the codes are too long to output in R script. So, I want to save it as code file (.R or .csv whatever) and will save in one folder. Then, I'll never lose it. Because I save excel files in different folders according to projects, and I lose it over time. – J.K Kim Commented Mar 5 at 19:51
 |  Show 1 more comment

1 Answer 1

Reset to default 3

Here are a few ways. A few more ways (fread/fwrite, RDS, feather, fst) can be found here.

# 1
dput(dataA, "dataA.R")
dataB <- dget("dataA.R")
identical(dataA, dataB)
## [1] TRUE

# 2
save(dataA, file = "dataA.Rdata")
load("dataA.Rdata", envir = e <- new.env())
identical(dataA, e$dataA)
## [1] TRUE

# 3
write.csv(dataA, "dataA.csv", row.names = FALSE)
dataB <- read.csv("dataA.csv")
identical(dataA, dataB)
## [1] TRUE

# 4
library(arrow)
write_parquet(dataA, normalizePath("dataA.parquet", mustWork = FALSE))
dataB <- read_parquet("dataA_parquet") |> as.data.frame()
identical(dataA, dataB)
## [1] TRUE

# 5
library(writexl)
library(readxl)
write_xlsx(dataA, "dataA.xlsx")
dataB <- read_xlsx("dataA.xlsx") |> as.data.frame()
identical(dataA, dataB)
## [1] TRUE

# 6
library(RSQLite)
con <- dbConnect(SQLite(), "dataA.sqlite")
dbWriteTable(con, "dataA", dataA)
dbDisconnect(con)
con <- dbConnect(SQLite(), "dataA.sqlite")
dataB <- dbReadTable(con, "dataA")
identical(dataA, dataB)
## [1] TRUE
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1745013923a279973.html

最新回复(0)