How to change a Decimal? to 3 decimal points U-SQL - u-sql

Thanks in advance for any help!
I have a column stored as a decimal? and I am trying to load it into another table and change it to 3 decimal points.
I have tried:
Math.Round(mycolumn, 3, MidpointRounding.AwayFromZero) AS my_column
This gives the error:
cannot convert from 'decimal?' to 'double'.
Many thanks,
BigD

The solution is to cast it to the proper type:
(decimal?)( Math.Round((decimal)mycolumn, 3, MidpointRounding.AwayFromZero)) AS mycolumn,

Related

R read_delim() changes values when reading data

I am trying to read in a tabstop seperated csv file using read_delim(). For some reason the function seems to change some field entries in integer values:
# Here some example data
# This should have 3 columns and 1 row
file_string = c("Tage\tID\tVISITS\n19.02.01\t2163994407707046646\t40")
# reading that data using read_delim()
data = read_delim(file_string, delim = "\t")
view(data)
data$ID
2163994407707046656 # This should be 2163994407707046646
I totally do not understand what is happening here. If I chnage the col type to character the entry stays the same. Does anyone has an explanation for this?
Happy about any help!
Your number has so many digits, that it does not fit into the R object. According to the specification IEEE 754, the precision of double is 53 bits which is approx. a number with 15 decimal digits. You reach that limit using as.double("2163994407707046646").

Create a simple range/values in range table

I am working on a simple project to help me get to know R, coming from javascript.
I have imported a list of numbers, and all I simply want to do, is to export a table that looks like the following:
"range","number"
"0.000-0.510",863
"0.510-1.020",21
"1.020-1.530",2
"1.530-2.040",2
"2.040-2.550",0
"2.550-3.059",2
"3.059-3.569",0
"3.569-4.079",3
"4.079->4.589",0
"4.589->5.099",1
where the ranges are in 10 steps, from the smallest to the largest value, the "range" and "number" are the top rows, and the columns going down are the different ranges and number of occurrences in this range.
This is my attempt so far:
list <- read.csv(file = "results/solarSystem.data")
table(list)
range <- (max(list) - min(list)) / 10
a1<-as.data.frame(table(cut(list,breaks=c(min(list),min(list)+1*range,min(list)+2*range,min(list)+3*range,min(list)+4*range,min(list)+5*range,min(list)+6*range,min(list)+7*range,min(list)+8*range,min(list)+9*range,max(list)))))
colnames(a1)<-c("range","freq")
a1
However, I get an error that
'Error in cut.default(list, breaks = c(min(list), min(list) + 1 * range...
'x' must be numeric'
This is the file I am importing, what looks like just a simple list of numbers, so I don't understand how it cannot be numeric?
https://gyazo.com/8fd00ce45c1c033f9dc9bf6c829195eb
Any advice on this would be appreciated!
Peter

converting decimal to hex in R

I want to convert a decimal number to hex format in a way that only bit corresponding to that decimal number is set. For example, for input 0, bit 0 should be set and results in
> paste("0x", sprintf("%032x",2^(0)),sep="")
[1] "0x00000000000000000000000000000001"
and for 1, bit one should be set, resulting in
> paste("0x", sprintf("%032x",2^(1)),sep="")
[1] "0x00000000000000000000000000000002"
This works till 30
> paste("0x", sprintf("%032x",2^(30)),sep="")
[1] "0x00000000000000000000000040000000"
but does not work for values larger than that
> paste("0x", sprintf("%032x",2^(32)),sep="")
Error in sprintf("%032x", 2^(32)) :invalid format '%032x'; use format %f, %e, %g or %a for numeric objects
Any idea how to get around this?
I think you're "overloading" the sprintf function. That is, your type is set to "%032x" and then you pass in the value 2^(32) which the function doesn't see as "%032x" so you get an error.
Here's a couple of semi-related questions, but I don't think these would count as exact duplicates:
why causes invalid format '%d in R?
hex to string formatting conversion in python
Why does a 32-bit OS support 4 GB of RAM?

Displaying large sequence of number for x axis in barplot

While plotting bargraph i want to display label of large sequence of numbers for x-axis.I figured by using names.arg=c() .But for displaying large sequence of numbers such as from 1 to 50 in x -axis i have to type all the numbers such as names.arg=c("1","2","3","4"------"50").
Is there any method to display such sequences by other method such as 1:50 or any other method so that i need not type all the numbers.
Can anyone help me to solve this problem.
Thanks in advance
You can use names.arg=1:50 or if you'd like them to have a text datatype names.arg=as.character(1:50)
(follow up from comments)

Keep the last 9 digits of an alphanumeric string in R

Please R-gurus, how can I keep the last 9 digits of an alphanumeric string
for e.g.
LA XAN 000262999444
RA XAN 000263000507
WA XAN 000263268038
SA XAN 000263000464
000263000463
000263000476
I only want to get
262999444
263000507
263268038
263000464
263000463
263000476
Thanks a lot
It's pretty easy in stringr because sub_str interprets negative indices as offsets from the end of the string.
library(stringr)
str_sub(xx, -9, -1)
If you just want the last 9 positions, you could just use substr:
substr(xx,nchar(xx) - 8,nchar(xx))
assuming that your character vector is stored in xx. Also, as Hadley notes below, nchar will return unexpected things if xx is a factor, not a character vector. His solution using stringr is definitely preferable.
Assuming the input is a vector named "strgs":
sub(".*(.........)$", "\\1", strgs)
#[1] "262999444" "263000507" "263268038" "263000464"
?sub
?regex
Not really sure which language you are looking for but here would be a c# implementation.
The logic would be something like :
string s = "WA XAN 000263268038";
s = s.Substring(s.Length - 10, 9);
Hope this helps!

Resources