简体   繁体   中英

How to assign value to zero object data.table? R

According to "FAQs about the data.table package in R", you can create a template of table, if you have data.table DT, by DT[0]. But when I try to assign some value to the column, it doesn't allowed me to do. This is what I have tried.

Binary.Table = matrix(0, nrow = 7, ncol = 26)
Binary.Table = data.table(Binary.Table)
setnames(Binary.Table, names(Binary.Table), c('JustDay', letters[1:25]))
Binary.Table[, JustDay := c("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday")]

I need to use this table as my template for my new table. So, I do this

DT.New <- Binary.Table[0]

Now, my DT.New is a zero obs data.table. Then I would like to assign day to column 'JustDay' (or another). I used

DT.New[, JustDay := c("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday")]

but it doesn't work in a way I need. Are there anything wrongs in my code? and How to do this? Thank you.

I see you have already accepted an answer, but anyway... I would do it like this:

ct <- c("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday")
setkey(DT.New, JustDay) # need to set the key before using i on the next line
DT.New[.(ct), nomatch=NA]
     JustDay  a  b  c  d  e  f  g  h  i  j  k  l  m  n  o  p  q  r  s  t  u  v  w  x  y
1:    Monday NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
2:   Tuesday NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
3: Wednesday NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
4:  Thursday NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
5:    Friday NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
6:  Saturday NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
7:    Sunday NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA

:= is designed to change values by reference , so you need to make sure there is something to reference (hence the nomatch=NA option; this is the default but I've included it for clarity's sake).

I have the feeling that it is tricky to add rows to data tables in the same way it works for data frames. You can do something like this to get it to work:

DT.template <- Binary.Table[0]
setkey(DT.template,JustDay)
DT.New <- DT.template[J(c("Monday", "Tuesday", "Wednesday", 
                          "Thursday", "Friday", "Saturday", "Sunday")),]

If the key column is not the first column of your template, it will be the first column of DT.New . In order to preserve the column order you can do the following:

numNewLines <- 7  # e.g. number of weekdays

setkeyv(DT.template,colnames(DT.template)[1])
createKey <- rep(new(typeof(DT.template[[1]]),NA),numNewLines)
DT.New <- DT.template[J(createKey),]

Once the necessary amount of rows is created, you can operate on them again in the usual way, eg

DT.New[,JustDay:=c("Monday", "Tuesday", "Wednesday", 
                    "Thursday", "Friday", "Saturday", "Sunday")]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM