Little useless-useful R function – DataFrame Maker

[This article was first published on R – TomazTsql, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Since the first little useless-useful R function MixedCases gain a lot of interest in the R community, let us not stop here. ????

In this blog-post let’s make another useless function, that would be somehow intriguing to data scientist. Function that can create a dummy data.frame. Again, the fun part is to make the function long and useless, yet still giving it some usefulness.

Following is the function to generate randomized dataframe using structure function.

DataFrameMaker <- function(col,row){
  command = "dd <- structure(list( "
  for (i in 1:col){
        var = paste("v",as.character(i),"= c(",sep="")
        command = paste(command, var ,sep = "")
        for (j in 1:row){
          a <- c(i*j)
          con = paste(a,sep="")
          if ((j < row) & (j %% row != 0)){
          command = paste(command, con,",",sep = "")
          else {
            command = paste(command, con,"), ",sep = "")
      rn = 'row.names = c('
      for(xx in 1:row){
        rn = paste(rn, xx, 'L,', sep = "")
        if (xx == row){rn = paste(substr(rn,1,nchar(rn)-1),')', sep = "")}

  command <- substr(command, 1, nchar(command)-2)
  command <- paste(command,"),", rn, "," ,"class = 'data.frame')",sep  = "")

Looks pretty long and useless, but it get’s the job done ???? When I input the following parameters:


I get the results:

In the background the function returns a string that is evaluated in last step of the function. The string holds:

"dd <- structure(list( v1= c(1,2), v2= c(2,4), v3= c(3,6), v4= c(4,8)),row.names = c(1L,2L),class = 'data.frame')"

Before you all go ranting about the useless code above and how long it is, here is a shorter version of it:

DataFrameMaker  <- function(col,row){
  dd <- matrix(nrow = row, ncol = col)
  for (i in 1:row) {
    for (j in 1:col) {
      dd[i, j] = (j*i)

That creates the same dataframe when called:

# Run the dataframe
dd <- DataFrameMaker(4,2)

Code is also available at Github. Feel free to update, improve or make the code even more useless ????

Happy Rrrr-ing!

To leave a comment for the author, please follow the link and comment on their blog: R – TomazTsql. offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)