coercion

Error when trying to store list in data.table of length 1

和自甴很熟 提交于 2021-02-11 08:13:40
问题 When trying to store a vector in a data.table, this works only when the data.table has length of more than one. Please find below a simplified version of the problem library(data.table) Working fine dt <- data.table( a = c("a", "b"), l = list()) dt$l[[1]] <- c(1:3) Results in: a l 1: a 1,2,3 2: b Producing Error dt <- data.table( a = c("a"), l = list()) dt$l[[1]] <- c(1:3) Error in [<-.data.table(x, j = name, value = value) : Supplied 3 items to be assigned to 1 items of column 'l'. The RHS

sprintf invalid format '%d'

若如初见. 提交于 2020-01-22 10:43:11
问题 This works: > sprintf('%d', c(1, 1.5)) [1] "1" "1" and this doesn't: > sprintf('%d', c(1.5, 1)) Error in sprintf("%d", c(1.5, 1)) : invalid format '%d'; use format %f, %e, %g or %a for numeric objects Why? 回答1: This is actually really interesting question. To start, %d stands for integer. The vector argument is recycled if possible but if it is c(1.5, 1) it will fail when sprintf() tries to replace %d with 1.5 (which is not integer). I thought it might be related to the fact that in R both

sprintf invalid format '%d'

纵然是瞬间 提交于 2020-01-22 10:42:18
问题 This works: > sprintf('%d', c(1, 1.5)) [1] "1" "1" and this doesn't: > sprintf('%d', c(1.5, 1)) Error in sprintf("%d", c(1.5, 1)) : invalid format '%d'; use format %f, %e, %g or %a for numeric objects Why? 回答1: This is actually really interesting question. To start, %d stands for integer. The vector argument is recycled if possible but if it is c(1.5, 1) it will fail when sprintf() tries to replace %d with 1.5 (which is not integer). I thought it might be related to the fact that in R both

Why doesn't a nested reference to an array coerce to a slice?

☆樱花仙子☆ 提交于 2020-01-11 05:15:30
问题 I read What are Rust's exact auto-dereferencing rules? from beginning to end, but I still have a question about the coercion from array to slice. Let us think about the following code: let arr: &[i32; 5] = &&&[1, 2, 3, 4, 5]; // let arr: &[i32] = &&&[1, 2, 3, 4, 5]; // Error; expected slice, found reference I would expect that &&&[1, 2, 3, 4, 5] has the type, &&&[i32; 5] and dereferences to &&[i32; 5] => &[i32; 5] => &[i32; 5] => &[i32] , but the result is different from what I expected. I

Rust Trait object conversion

人盡茶涼 提交于 2020-01-09 11:40:08
问题 The following code won't compile due to two instances of this error: error[E0277]: the trait bound Self: std::marker::Sized is not satisfied I don't understand why Sized is required in this instance as both &self and &Any are pointers and the operation does not require knowledge of the size of the structure that implements the trait, it only requires knowledge of the pointer itself and the type it is converting from and to, which it will have because &self is generic when implemented inside a

stdClass Object has strange quirk on live site but not local machine [closed]

♀尐吖头ヾ 提交于 2020-01-04 04:44:06
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed last year . I created a set of helper functions to make my life easier when doing database operations in php. Essentially I pass the functions an stdClass Object that they then use to either perform an operation (delete, add, etc) or retrieve information. They seem to work as intended except for the helper function that is

Compare strings as numbers in SQLite3

∥☆過路亽.° 提交于 2020-01-03 08:39:12
问题 I have the following query in SQLite: SELECT * FROM t1 ORDER BY t1.field Where t1.field is a text column containing numbers. Is it posible to force SQLite to consider the values of t1.field as numbers instead of strings (whithout doing ALTER TABLE )? Right now the sort is a pure string one, so 10 goes before 2. Thank you. 回答1: Well, found a solution: SELECT * FROM t1 ORDER BY t1.field + 0 The + 0 part seems to force conversion to number 来源: https://stackoverflow.com/questions/4204319/compare

JavaScript Implicit Coercion

社会主义新天地 提交于 2020-01-02 05:15:13
问题 I'm reading a JavaScript tutorial on implicit and explicit coercion. What happens in the background with respect to implicit coercion? var a = "42"; var b = a * 1; //this is implicitly coerced to 42 -- the number Does implicit coercion ALWAYS coerce to a number? What if we wanted to do something a per the below Python example. I'm getting confused because other languages such as Python would give you a result as per below. a = "3"; b = 9; print a * b; //This would print 333333333 -- the

Why does 1..99,999 == “1”..“99,999” in R, but 100,000 != “100,000”?

可紊 提交于 2019-12-29 01:34:55
问题 In the console, go ahead and try > sum(sapply(1:99999, function(x) { x != as.character(x) })) 0 For all of values 1 through 99999, "1" == 1 , "2" == 2 , ..., 99999 == "99999" are TRUE . However, > 100000 == "100000" FALSE Why does R have this quirky behavior, and is this a bug? What would be a workaround to, e.g., check if every element in an atomic character vector is in fact numeric? Right now I was trying to check whether x == as.numeric(x) for each x , but that fails on certain datasets