Reputation: 9796
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!
Upvotes: 0
Views: 6958
Reputation: 5
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/
Upvotes: 0
Reputation: 44525
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))
Upvotes: 3
Reputation: 89
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.
Upvotes: 0