0

I would like to insert a blank column in between "Delta = delta" and "Card = vars" in the dataframe below. I would also like to sort the output by the column "Model_Avg_Error" in the dataframe as well.

df = data.frame(Card = vars, Model_Avg_Error = model_error, Forecast = forecasts,  Delta = delta, ,Card = vars, Model_Avg_Error = model_error, 
                Forecast = forecasts,  Delta = delta)

# save
write.csv(df, file = file.path(proj_path, "output.csv"), row.names = F)

This was the error received from above:

Error in data.frame(Card = vars, Model_Avg_Error = model_error, Forecast = forecasts, : argument is missing, with no default

ZJAY
  • 2,057
  • 8
  • 25
  • 48

2 Answers2

6

You can add your blank column, re-order, and sort using the code below:

df$blankVar <- NA #blank column 
df[c("Card", "blankVar", "Model_Avg_Error", "Forecast", "Delta")] #re-ordering columns by name
df[order(df$Model_Avg_Error),] #sorting by Model_Avg_Error
juliamm2011
  • 126
  • 3
3

Here's a general way to add a new, blank column

library(tibble)

# Adds after the second column
iris %>% add_column(new_col = NA, .after = 2)

# Adds after a specific column (in this case, after Sepal.Width)
iris %>% add_column(new_col = NA, .after = "Sepal.Width")
stevec
  • 27,285
  • 13
  • 133
  • 181