[datatable-help] filing a column with a number from another column by group

carlsutton suttoncarl at ymail.com
Thu Dec 1 18:21:46 CET 2016


As a result of some computations using data.table wherein the computations
require using "i" to select certain rows, the results of the computations
only reside in those certain rows.  Further computations require that the
computation results be in each row by group.  A much simplified example
follows.  

help_request_120116.R
<http://r.789695.n4.nabble.com/file/n4726956/help_request_120116.R>  

Not being familiar with the nabble upload feature, I have also done a cut
and paste.

#  adding data to data.table column
library(data.table)
dt1 <- data.table(group = rep(1:5, each = 3),b = rep(1:3,times = 5),
                  var   = c(NA,NA,NA,20,NA,NA,21,NA,NA,NA,NA,NA,22,NA,NA),
                  var1 = c(NA,NA,NA,20,NA,NA,20,NA,NA,NA,NA,NA,20,NA,NA))
dt1
#  Result Wanted
#    group b var var1
#1:      1 1  NA   20
#2:      1 2  NA   20
#3:      1 3  NA   20
#4:      2 1  20   20
#5:      2 2  NA   20
#6:      2 3  NA   20
#7:      3 1  21   20
#8:      3 2  NA   20
#9:      3 3  NA   20
#10:     4 1  NA   20
#11:     4 2  NA   20
#12:     4 3  NA   20
#13:     5 1  22   20
#14:     5 2  NA   20
#15:     5 3  NA   20

I have searched the web, books, vignette, FAQs and fumbled around trying
this and that for the last week or so with no success.  The solution is
probably very simple and I hate to waste your time with something this
basic, but I just cannot get my head wrapped around how to this.

BTW, love the package, use setkey and by all the time.

Carl Sutton




-----
Carl Sutton
--
View this message in context: http://r.789695.n4.nabble.com/filing-a-column-with-a-number-from-another-column-by-group-tp4726956.html
Sent from the datatable-help mailing list archive at Nabble.com.


More information about the datatable-help mailing list