coercion

How does JS type coercion work?

一曲冷凌霜 提交于 2019-11-27 02:16:08
I'm learning about == vs. === and came across this answer which was very helpful in understanding the concept. However I wondered about one of the examples: '0' == false // true This might make sense, since == doesn't check for type. But then I tried some possible coercions in the console and found the following: Boolean('0') // true String(false) // "false" I would have thought '0' == false has the same truth value as '0' === String(false) , but that doesn't seem to be the case. So how does the coercion actually work? Is there a more basic type I'm missing? "0" is a string containing the

Converting two columns of a data frame to a named vector

淺唱寂寞╮ 提交于 2019-11-27 02:07:30
问题 I need to convert a multi-row two-column data.frame to a named character vector. My data.frame would be something like: dd = data.frame(crit = c("a","b","c","d"), name = c("Alpha", "Beta", "Caesar", "Doris") ) and what I actually need would be: whatiwant = c("a" = "Alpha", "b" = "Beta", "c" = "Caesar", "d" = "Doris") 回答1: Use the names function: whatyouwant <- as.character(dd$name) names(whatyouwant) <- dd$crit as.character is necessary, because data.frame and read.table turn characters into

Forced conversion of non-numeric numpy arrays with NAN replacement

一曲冷凌霜 提交于 2019-11-26 22:25:00
问题 Consider the array x = np.array(['1', '2', 'a']) Tying to convert to a float array raises an exception x.astype(np.float) ValueError: could not convert string to float: a Does numpy provide any efficient way to coerce this into a numeric array, replacing non-numeric values with something like NAN? Alternatively, is there an efficient numpy function equivalent to np.isnan , but which also tests for non-numeric elements like letters? 回答1: You can convert an array of strings into an array of

Why does the Linq Cast<> helper not work with the implicit cast operator?

孤街醉人 提交于 2019-11-26 20:42:07
问题 Please read to the end before deciding of voting as duplicate... I have a type that implements an implicit cast operator to another type: class A { private B b; public static implicit operator B(A a) { return a.b; } } class B { } Now, implicit and explicit casting work just fine: B b = a; B b2 = (B)a; ...so how come Linq's .Cast<> doesn't? A[] aa = new A[]{...}; var bb = aa.Cast<B>(); //throws InvalidCastException Looking at the source code for .Cast<> , there's not much magic going on: a few

Convert factor to integer [duplicate]

纵饮孤独 提交于 2019-11-26 15:18:07
This question already has an answer here: How to convert a factor to integer\numeric without loss of information? 7 answers I am manipulating a data frame using the reshape package. When using the melt function, it factorizes my value column, which is a problem because a subset of those values are integers that I want to be able to perform operations on. Does anyone know of a way to coerce a factor into an integer? Using as.character() will convert it to the correct character, but then I cannot immediately perform an operation on it, and as.integer() or as.numeric() will convert it to the

Why this behavior when coercing a list to character via as.character()?

混江龙づ霸主 提交于 2019-11-26 14:23:59
问题 In the process of (mostly) answering this question, I stumbled across something that I feel like I really should already have seen before. Let's say you've got a list: l <- list(a = 1:3, b = letters[1:3], c = runif(3)) Attempting to coerce l to various types returns an error: > as.numeric(l) Error: (list) object cannot be coerced to type 'double' > as.logical(l) Error: (list) object cannot be coerced to type 'logical' However, I'm apparently allowed to coerce a list to character, I just wasn

What is the difference between a slice and an array?

孤者浪人 提交于 2019-11-26 13:57:17
问题 Why are both &[u8] and &[u8; 3] ok in this example? fn main() { let x: &[u8] = &[1u8, 2, 3]; println!("{:?}", x); let y: &[u8; 3] = &[1u8, 2, 3]; println!("{:?}", y); } The fact that &[T; n] can coerce to &[T] is the aspect that makes them tolerable. — Chris Morgan Why can &[T; n] coerce to &[T] ? In what other conditions does this coercion happen? 回答1: [T; n] is an array of length n , represented as n adjacent T instances. &[T; n] is purely a reference to that array, represented as a thin

Why does “one” < 2 equal FALSE in R?

醉酒当歌 提交于 2019-11-26 11:35:45
问题 I\'m reading Hadley Wickham\'s Advanced R section on coercion, and I can\'t understand the result of this comparison: \"one\" < 2 # [1] FALSE I\'m assuming that R coerces 2 to a character, but I don\'t understand why R returns FALSE instead of returning an error. This is especially puzzling to me since -1 < \"one\" # TRUE So my question is two-fold: first, why this answer, and second, is there a way of seeing how R converts the individual elements within a logical vector like these examples?

How does JS type coercion work?

坚强是说给别人听的谎言 提交于 2019-11-26 10:00:23
问题 I\'m learning about == vs. === and came across this answer which was very helpful in understanding the concept. However I wondered about one of the examples: \'0\' == false // true This might make sense, since == doesn\'t check for type. But then I tried some possible coercions in the console and found the following: Boolean(\'0\') // true String(false) // \"false\" I would have thought \'0\' == false has the same truth value as \'0\' === String(false) , but that doesn\'t seem to be the case.

Convert factor to integer [duplicate]

[亡魂溺海] 提交于 2019-11-26 04:21:28
问题 This question already has an answer here: How to convert a factor to integer\\numeric without loss of information? 7 answers I am manipulating a data frame using the reshape package. When using the melt function, it factorizes my value column, which is a problem because a subset of those values are integers that I want to be able to perform operations on. Does anyone know of a way to coerce a factor into an integer? Using as.character() will convert it to the correct character, but then I