2

I have to maintain some R code and the guy that wrote it isn't here anymore.

I've found some weird sql statements in his code and i am not sure what they mean. This is one of them:

sqldf(paste("select ", i," i , * from simTypeFile union all select * from simTypeFiles", sep=""))

i is an index that is incremented from zero to x. I would expect that a select keyword will be followed by a column_name or an asterisk. In this case it is followed by an integer i, a space, a string i and an asterisk.

The table that is to be queried doesn't have any column with a number i or a string "i". What do those characters following the select statement mean?

0

1 Answer 1

1

I think they are preparing data for simulation, and adding i column for indexing and naming it as i (to make it explicit use AS to name a column, e.g.: select 1 AS i), consider below example:

# emprty data.frame to fill in
simTypeFiles <- data.frame(i = numeric(0), speed = numeric(0), dist = numeric(0))

# dummy data
simTypeFile <- cars[1:3,]

# then loop and rbind - union
for(i in 1:3){
  simTypeFiles <- 
    rbind(
      simTypeFiles,
      sqldf(paste("select ", i," i , * from simTypeFile union all select * from simTypeFiles", sep = ""))
    )
}

simTypeFiles
Sign up to request clarification or add additional context in comments.

1 Comment

Ommiting rbind() and using something along simTypeFiles <- sqldf(paste("select ", i," i , * from simTypeFile union all select * from simTypeFiles", sep = "")) with print() inside the loop shows this even better.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.