简体   繁体   English

如何为零对象data.table赋值? [R

[英]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]. 根据“关于R中data.table软件包的常见问题解答”,如果您有data.table DT,则可以通过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. 现在,我的DT.New是一个零obs data.table。 Then I would like to assign day to column 'JustDay' (or another). 然后,我想将天分配给“ JustDay”列(或另一个)。 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). :=旨在通过reference更改值,因此您需要确保要引用一些内容(因此, nomatch=NA选项;这是默认选项,但是为了清楚起见,我将其包括在内)。

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 . 如果键列不是模板的第一列, DT.NewDT.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")]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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