0
votes

I have multiple data tables that have many rows of student data such that the same student can be on several rows with minor differences in some columns, ie:

     FirstName     LastName   HS      Uni Semester Graduated
     -Suzy     Heddenbocher  CHS     BigU    1         N
     -Suzy     Heddenbocher  CHS     BigU    2         N
     -Ed       Heddenbocher  NHS     SWSt    1         N

I ordered the data table with setkey(Y1, LastName, FirstName) and then tried to set uniqid by using

Y1[, uniqid:=.GRP, by=key(Y1)]  

I get consistent errors:

Error in [.data.table(Y1, , :=(uniqid, .GRP), by = key(Y1)) : Type of RHS ('integer') must match LHS ('double'). To check and coerce would impact performance too much for the fastest cases. Either change the type of the target column, or coerce the RHS of := yourself (e.g. by using 1L instead of 1)

NB: Thank you for your help. I had no idea what a "double precision" vector was until you told me that was what "double" meant, but it was a column class issue. I solved the problem by running this code:

setkey(Y3, "Last Name", "First Name")
Y3$uniqid <- as.integer(Y3$uniqid)
Y3[, uniqid:=.GRP, by=key(Y3)]
1

1 Answers

2
votes

It would appear [without a reproducible example] that you have already defined uniqid within your data.table Y1, and that the previous definition was as a double precision vector [that is what double means here, nothing to do with data.table keys].

A small example:

a <- data.table(l =letters[1:5],i = rep(seq_len(10),5),j= letters[21:25])
setkey(a,l,i)
# id set as a non-integer numeric value 
a[, id := 5.2]
[,id := .GRP,by=key(a)]
# Error in `[.data.table`(a, , `:=`(id, .GRP), by = key(a)) : 
#  Type of RHS ('integer') must match LHS ('double'). To check and coerce would impact 
#   performance too much for the fastest cases. Either change the type of the target column, or 
#   coerce the RHS of := yourself (e.g. by using 1L instead of 1)


# set to a non-existing column
a[, unique.id := .GRP, by = key(a)]
head(a)
     l i j  id unique.id
# 1: a 1 u 5.2         1
# 2: a 1 u 5.2         1
# 3: a 1 u 5.2         1
# 4: a 1 u 5.2         1
# 5: a 1 u 5.2         1
# 6: a 6 u 5.2         2