How to assign zero values to multiple columns in a data.frame in r within a loop -
i have loop splits dataset (fulldataset) training (olddata) , holdout (newdata)dataset. model (auto.arima (xtreg)regressors) estimated on 24 pieces indexed i. holdout dataset (newdata) used make predictions using fit model built on olddata. newdata2 exact same newdata, except want 3 of predictors in columns 18:20 take on 0 values. when run loop, no errors, newdata2's variables in column's 18 thru 20 have same original values, , predictions same using both newdata , newdata2. when specify variables name assign vector of zeros first specified variable.
olddata<-fulldataset[[i]][1:525,] newdata<-fulldataset[[i]][526:547,] newdata2<-transform(fulldataset[[i]][526:547,],fulldataset[[i]][526:547,18:20]<-0 )
my question is: how transform dataframe can assign 0 values more 1 column in dataframe? also, remember code running in loop. used plyr , apply functions needs loop executes faster.
1) don't use <-
inside argument function. use =
instead.
2) second argument of transform
needs column name.
3) easiest solution problem in 2 steps:
newdata2 = fulldataset[[i]][526:547,] newdata2[,18:20] = 0
Comments
Post a Comment