Questions tagged [coercion]

Coercion, type conversion and typecasting are different ways of, implicitly or explicitly, changing an entity of one data type into another.

Coercion, type conversion and typecasting are different ways of, implicitly or explicitly, changing an entity of one data type into another.

398 questions
0
votes
1 answer

Coercion with NULL producing different results in javascript

Just for fun, In console, I did this: Number(null); Result comes 0 Then again I did this: null == 0; Result comes false; Why is this strange behavior? I expected true based on the above example.
marukobotto
  • 759
  • 3
  • 12
  • 26
0
votes
1 answer

C# datatype coercion in static size

So if I define a some constants const byte MAX_SIZE = 100; And then instantiate an integral array with the static size above int[] myArr = new int[MAX_SIZE]; Is the static size explicit coercion or this perfectly acceptable. Just curious as I'm…
EasyBB
  • 6,176
  • 9
  • 47
  • 77
0
votes
2 answers

R: How to convert factors into numeric for a DATA FRAME?

I want to convert factors into numeric. I know this a FAQ, I tried already as.numeric(levels(f))[f] and as.numeric(as.character(f)). But this doesn't help as I want to convert all columns (more than 1000 and all of type factor) of the df into…
N.Varela
  • 910
  • 1
  • 11
  • 25
0
votes
1 answer

Scraping a web link with Rvest

I am trying to figure out a way to extract a link from a wiki page. The web page is the following: wiki <- html("https://en.wikipedia.org/wiki/Category:1879_births") I am interested by the following link: v = html_node(wiki,…
Jb_Eyd
  • 635
  • 1
  • 7
  • 20
0
votes
2 answers

R - Adding numbers within a data frame cell together

I have a data frame in which the values are stored as characters. However, many values contain two numbers that need to be added together. Example: 2014 Q1 Sales 2014 Q2 Sales 2014 Q3 Sales 2014 Q4 Sales Product 1 …
rob
  • 13
  • 2
0
votes
1 answer

Force coercion in Moose

I want to modify an attribute's value every time it is set, no matter if it is done within constructor or by a 'writer'(i don't use 'builder' or 'default' in that case). Basically the attribute(not necessary 'Str' type) is passed to the constructor…
roland16
  • 27
  • 4
0
votes
3 answers

Why javascript console.log(1 + + " " + 3); sentence returns 4 and not "1 3"?

When the statement console.log(1 + + " " + 3); is executed in the Chrome console, the result is 4 and not "1 3" as i would expect. Can someone please explain why this is the case?
Eddiev
  • 13
  • 2
0
votes
1 answer

JavaScript Coercion: Console vs Script

today, I encountered something rather strange. Consider the following code (which I use to sync a range slider with an input element): // val = "50" // other_val = 100 if( val > other_val ){ console.log(val + '>' + other_val); // => 50 > 100 …
CunningFatalist
  • 453
  • 1
  • 9
  • 21
0
votes
1 answer

Is type coercion more dangerous inside a conditional?

I just read this in a blog post: If you want to coerce values, do so outside of the condition not within it. I'm curious. In Javascript, is it worse to use type coercion in a conditional than outside of one? Are there any side effects? Example…
RichLitt
  • 352
  • 1
  • 3
  • 13
0
votes
3 answers

os.mkdir(MyClass()), TypeError: coercing to Unicode, magic method

I want to write a class, an instance of which should be usable as an argument of os.mkdir(). Is there a magic method, which I could add to my class, so that this would work? The following implementation with __unicode__(): main.py: import os class…
user
  • 1
  • 2
0
votes
1 answer

In lapply(list(...), ._java_valid_object) : NAs introduced by coercion

a[1] [1] "AA" > rhive.query("select count( uniquecarrier) from f08 where uniquecarrier= '",a[1] ," ' ") Error: java.sql.SQLException: Query returned non-zero code: 40000, cause: FAILED: ParseException line 1:61 cannot recognize input near ''…
0
votes
0 answers

R coerce multiple columns in frame

I have a data frame, call it f, and I want to coerce the first three columns from factors to character strings. f[,1] = as.character(f[,1]) seems to work (and with columns 2 and 3). But when I try to do this: f[,1:3] = as.character(f[,1:3]) all…
0
votes
2 answers

In Ruby, can the coerce() method know what operator it is that requires the help to coerce?

In Ruby, it seems that a lot of coerce() help can be done by def coerce(something) [self, something] end that's is, when 3 + rational is needed, Fixnum 3 doesn't know how to handle adding a Rational, so it asks Rational#coerce for help by…
nonopolarity
  • 146,324
  • 131
  • 460
  • 740
0
votes
1 answer

Convert selected dataframe column to another class

I would like to add a vector of character class to a dataframe without affecting the type of class of the other columns. For example, the dataframe mtcars consists only of numeric types of columns and if I add an additional column like this, m <-…
Wolfgang
  • 55
  • 1
  • 6
0
votes
3 answers

R correlation matrix by group using spearman

I am trying to create a set of correlation matrices by different levels of a factor variable. This question has previously been answered (spearman correlation by group in R) but not for a matrix and the vector result doesn't seem to generalize as…
Impossible9
  • 101
  • 7