I want to add a new row to an existing data frame, using a function like this:
df <- data.frame(num1 = numeric(),
num2 = numeric(),
int1 = integer(),
chr1 = character(),
stringsAsFactors=FALSE)
filler <- function () {
newrow = c(0.1, 0.01, 100, "char")
df[nrow(df)+1, ] <- newrow
assign(x = "df", value = df, envir = .GlobalEnv)
}
filler
but nothing is happening.
I'd like to get the following output:
> df
num1 num2 int1 chr1
1 0.1 0.01 100 char
Note: I don't want to define the new table within a function, because I want to rerun the function afterwards with different "newrow" values.
characterBTW:c(0.1, 0.01, 100, "char").dfinto the function andreturnthe modified data frame to have the function fit into the functional programming paradigm. Or have the function not touch the data frame and just return a 1-line data frame of the row to add. In either case, just userbind()inside the function or out of it.