我想在" Delta = delta"之间插入一个空白列。和"卡= vars"在下面的数据框中。我还想按照" Model_Avg_Error"列对输出进行排序。在数据框中也是如此。
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)
这是从上面收到的错误:
data.frame出错(Card = vars,Model_Avg_Error = model_error,Forecast = forecast,: 参数缺失,没有默认值
答案 0 :(得分:3)
这是添加新的空白列的一般方法
library(dplyr)
# 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")
答案 1 :(得分:2)
您可以使用以下代码添加空白栏,重新排序和排序:
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