0

CREATING AN EMPTY DATA FRAME:

data <- data.frame(ticks = numeric(0), identity = numeric(0), p_h = numeric(0), p_y = numeric(0), v_x = numeric(0), v_y = numeric(0), size = numeric(0), homo = numeric(0))

ADDING DATA TO DATA FRAME:

while (x < timeStepsToRun)
{
.....
data[i, ] <- c(ag$ticks, ag$who, ag$xcor, ag$ycor, ag$v-x, ag$v-y,"10","1")
i=i+1;
...
}

THOUGH I GET THE FOLLOWING ERROR WHEN ADDING DATA:

Error in value[[jvseq[[jjj]]]] : subscript out of bounds
In addition: Warning message:
In matrix(value, n, p) : data length exceeds size of matrix

Please suggest a better strategy or help me in correcting the above. Thanks in advance!

Abhishek Bhatia
  • 9,404
  • 26
  • 87
  • 142
  • It is a part of a loop. Please check now. – Abhishek Bhatia Jul 31 '14 at 05:47
  • 1
    This is not reproducible. See: [How to make a great r reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – Jota Jul 31 '14 at 05:49
  • 6
    If you ever find yourself creating an empty data.frame and filling it row-by-row, STOP. This is almost never the way to go about things in R and is usually inefficient in time and lines of code. Please read a bit of introductory R material before asking more questions. – thelatemail Jul 31 '14 at 05:59
  • @thelatemail I know a bit about R but am stuck. Another approach I can think of is to combine dataframes. Apart from that I don't know anything else. What i am trying accomplish is I link to software carry each simulation step once and then add data to R after each step. So normal R scenarios are of not use as you can understand. – Abhishek Bhatia Jul 31 '14 at 07:21
  • My guess is that `length(data[i, ]) != length(c(ag$ticks, ag$who, ag$xcor, ag$ycor, ag$v-x, ag$v-y,"10","1"))` or `timeStepsToRun > nrow(data)` – David Arenburg Jul 31 '14 at 07:41

3 Answers3

3

If you know how large you need your data.frame to be, prespecify the size, then you won't encounter these kind of errors:

rows <- 1e4 # it's not clear how many you actually need from your example
data <- setNames(as.data.frame(matrix(nrow = rows, ncol = 8))
                 c('ticks', 'identity', 'p_h', 'p_y', 'v_x', 'v_y', 'size', 'homo'))

Then you can fill it in the way that you describe. Even creating a dataframe larger than the one you need and cutting it down to size later is more efficient than growing it row by row.

If you know the classes of the columns you are going to create it can also be performance-enhancing to prespecify the column classes:

rows <- 1e4
data <- data.frame(ticks = integer(rows), 
                   identity = character(rows), 
                   p_h = numeric(rows), 
                   p_y = numeric(rows), 
                   v_x = numeric(rows), 
                   v_y = numeric(rows), 
                   size = numeric(rows), 
                   homo = numeric(rows))
Thomas
  • 43,637
  • 12
  • 109
  • 140
0

I gotchu:

data <- data.frame(ticks=NA, identity=NA, p_h=NA, p_y=NA, v_x=NA, v_y=NA, 
                  size=NA, homo=NA)

timeStepstoRun <- 10
x <- #something
i <- 1

while (x < timeStepstoRun) {
  data[i,] <- 1:8
  i <- i + 1
}

Just replace timeStepstoRun, x, and data[x,] <- ... with whatever you actually have. This is never gonna be the good way to do what you're trying to do, but I thought I'd just throw it out.

psadosky
  • 89
  • 5
  • I don't get your answer x is incremented after each while loop here. The second point behind how can I write all columns of a row with a single statement here? – Abhishek Bhatia Jul 31 '14 at 07:04
  • Just replace `1:8` above with whatever you want the row to be. Btw if you could add in the rest of your code, maybe with some of your data (like what `ag` and `x` are), that'd be really helpful. – psadosky Jul 31 '14 at 07:12
  • What i am trying accomplish is I link to software carry each simulation step once and then add data to R. ag is dataframe where i store it each time x is the number rows/times i access the software, run it and read data. – Abhishek Bhatia Jul 31 '14 at 07:22
0

My personal favorite solution:

# Create data frame with 0 rows and 3 columns.
df <- data.frame(matrix(ncol = 3, nrow = 0))

# Provide column names.
colnames(df) <- c('var1', 'var2', 'var3')

# Add the row.
df[nrow(df) + 1,] = c("a", "b", "c")

Once you have this simplified version working, you can adapt it to your while loop.

Source: https://www.statology.org/create-empty-data-frame-in-r/

kig
  • 5
  • 3