oivemaria
oivemaria

Reputation: 463

Adding/creating data to empty dataframe

I created an empty dataframe with 3 columns and then attempted to add values to individual columns row by row and i get an error that I can't resolve. I have about 300 entries that I've all hardcoded like the set below, so I'd like for them all to work but the code won't even work for one row of data.

DF<- data.frame(Code=character(),Value=integer(),Period=character(),stringsAsFactors=FALSE)
DF$Code[1]<-'Code_A'
DF$Value[1]<-Input_Data$Value[Input_Data$Code=='Code_A']
DF$Period<-paste('Quarter1')

Error in `$<-.data.frame`(`*tmp*`, "Unique_Cell_Code", value = "B3283") : 
  replacement has 1 row, data has 0

Upvotes: 0

Views: 1223

Answers (1)

Paul
Paul

Reputation: 1355

This will work:

DF[1,] <- c('Code_A', Input_Data$Value[Input_Data$Code=='Code_A'], paste('Quarter1'))

Upvotes: 1

Related Questions