Home > Blockchain >  Error adding multiple columns to R dataframe
Error adding multiple columns to R dataframe

Time:11-01

I have a dataframe dept_sales

  Store 2010-02-19 2010-02-26 2010-03-05 2010-03-19 2010-05-14 2010-12-10
1     2       0.78       7.02       0.78       0.78          0       0.00
2     4       0.00       0.00       0.00       0.00          0       1.56
3    18       0.00       0.00       0.00       0.00         28       0.0

I am trying to add multiple columns to this dataframe all with value 0. I did this

dept_sales[, dropped_columns] = 0

where dropped_columns is just a list of dates:

 [1] "2010-02-05" "2010-02-12" "2010-03-12" "2010-03-26" "2010-04-02" "2010-04-09" "2010-04-16" "2010-04-23" "2010-04-30" "2010-05-07" "2010-05-21"
[12] "2010-05-28" "2010-06-04" "2010-06-11" "2010-06-18" "2010-06-25" "2010-07-02" "2010-07-09" "2010-07-16" "2010-07-23" "2010-07-30" "2010-08-06"
[23] "2010-08-13" "2010-08-20" "2010-08-27" "2010-09-03" "2010-09-10" "2010-09-17" "2010-09-24" "2010-10-01" "2010-10-08" "2010-10-15" "2010-10-22"
[34] "2010-10-29" "2010-11-05" "2010-11-12" "2010-11-19" "2010-11-26" "2010-12-03" "2010-12-17" "2010-12-24" "2010-12-31" "2011-01-07" "2011-01-14"
[45] "2011-01-21" "2011-01-28" "2011-02-04" "2011-02-11" "2011-02-18" "2011-02-25"

which I get error

Error in `[<-.data.frame`(`*tmp*`, , dropped_columns, value = 0) : 
  new columns would leave holes after existing columns

CodePudding user response:

We can mimic that error by going a bit extreme:

dept_sales[, 10000] <- 0
# Error in `[<-.data.frame`(`*tmp*`, , 10000, value = 0) : 
#   new columns would leave holes after existing columns

It appears that your dropped_columns may be real Dates instead of strings. Convert to strings first.

dept_sales[,dropped_columns] <- 0
# Error in `[<-.data.frame`(`*tmp*`, , dropped_columns, value = 0) : 
#   new columns would leave holes after existing columns
dept_sales[,as.character(dropped_columns)] <- 0
dept_sales[,1:16]  # just a subset of columns for demonstration here
#   Store 2010-02-19 2010-02-26 2010-03-05 2010-03-19 2010-05-14 2010-12-10 2010-02-05 2010-02-12 2010-03-12 2010-03-26 2010-04-02 2010-04-09 2010-04-16 2010-04-23 2010-04-30
# 1     2       0.78       7.02       0.78       0.78          0       0.00          0          0          0          0          0          0          0          0          0
# 2     4       0.00       0.00       0.00       0.00          0       1.56          0          0          0          0          0          0          0          0          0
# 3    18       0.00       0.00       0.00       0.00         28       0.00          0          0          0          0          0          0          0          0          0

Data

dept_sales <- structure(list(Store = c(2L, 4L, 18L), "2010-02-19" = c(0.78, 0, 0), "2010-02-26" = c(7.02, 0, 0), "2010-03-05" = c(0.78, 0, 0), "2010-03-19" = c(0.78, 0, 0), "2010-05-14" = c(0L, 0L, 28L), "2010-12-10" = c(0, 1.56, 0)), class = "data.frame", row.names = c("1", "2", "3"))
dropped_columns <- structure(c(14645, 14652, 14680, 14694, 14701, 14708, 14715, 14722, 14729, 14736, 14750, 14757, 14764, 14771, 14778, 14785, 14792, 14799, 14806, 14813, 14820, 14827, 14834, 14841, 14848, 14855, 14862, 14869, 14876, 14883, 14890, 14897, 14904, 14911, 14918, 14925, 14932, 14939, 14946, 14960, 14967, 14974, 14981, 14988, 14995, 15002, 15009, 15016, 15023, 15030), class = "Date")
  • Related