0

I am distributing an R code script for a demonstration, and need to edit the raw data embedded within the script. The data is entered into a series of numerical vectors, which get loaded into a data frame. Problem is, the raw data is also my experimental data, and I would rather not disseminate it.

It is easy enough to transform the data once it is loaded into R. However, I want to output that transformed data in a format that I can easily copy/paste back into my original source code in place of the original data. I think I can do this with a combination of paste() and gsub() within a forloop iterating through the data frame, but I was wondering if there might an easier way. This thread had me looking at the dump() function, but I haven't figured out how to make it work yet. Any other ideas? Thanks!

As an illustration, here is the format my code is in:

> dat1<-c(4,6,7,8,2,4,5,9)
> dat2<-c(7,7,6,7,8,5,5,4)
> df<-data.frame(dat1,dat2)
> df
  dat1 dat2
1    4    7
2    6    7
3    7    6
4    8    7
5    2    8
6    4    5
7    5    5
8    9    4

After transforming the data, I need to output the contents of df in a format that I can insert into the source code in place of dat1<-c(4,6,7,8,2,4,5,9)

Community
  • 1
  • 1
user3658874
  • 77
  • 1
  • 1
  • 4

1 Answers1

1

dput "writes an ASCII text representation of an R object"

> dput(df)
structure(list(dat1 = c(4, 6, 7, 8, 2, 4, 5, 9), dat2 = c(7, 
7, 6, 7, 8, 5, 5, 4)), .Names = c("dat1", "dat2"), row.names = c(NA, 
-8L), class = "data.frame")

> rm(df)
> df <- structure(list(dat1 = c(4, 6, 7, 8, 2, 4, 5, 9), dat2 = c(7, 
+ 7, 6, 7, 8, 5, 5, 4)), .Names = c("dat1", "dat2"), row.names = c(NA, 
+ -8L), class = "data.frame")
> df
  dat1 dat2
1    4    7
2    6    7
3    7    6
4    8    7
5    2    8
6    4    5
7    5    5
8    9    4
GSee
  • 48,880
  • 13
  • 125
  • 145