I want to take the minimum of minute values.
> typeof(minutes(7))
[1] "double"
> min(c(minutes(7),minutes(8)))
[1] 0
> seconds(min(as.numeric(c(minutes(7),minutes(8)))))
[1] "420S"
Why is that?
After all it works with regular doubles:
> typeof(c(7.0,8.0))
[1] "double"
> min(c(7.0,8.0))
[1] 7
Related
How can that be?
> mode(daten[1,16])
[1] "numeric"
> mode(weku)
[1] "numeric"
>
> weku
[1] 10.47855
> daten[1,16]
[1] 814995955
> daten[1,16]/weku
[1] 77777557
>
> 814995955/10.47855
[1] 77777551
>
I don't understand this. How can I get the correct calculation?
daten[1,16]/weku is correct.
R does not display all of the decimal values it stores internally. What is printed on the console is controlled by options("digits").
For example, compare print(pi), print(pi, digits=10), and print(pi, digits=22).
> 52631579 - 52631559
[1] 20
> 20 / 52631559
[1] 3.800001e-07
> 52631579 / 52631559
[1] 1
> class(52631579 - 52631559)
[1] "numeric"
I would like the division to result in the precise float.
you can use sprintf function to format output
sprintf("%.50f", 52631579 / 52631559)
# [1] "1.00000038000014401795567664521513506770133972167969"
I have a simple question. I have two Date objects in R that are supposed to be identical (they have the same value and class), but R is saying they are not equal. I am running on linux though I get the same result on a windows machine. Why is this happening?
code:
start=as.Date("2014-12-31")
finish=as.Date("2014-11-28")
dates = seq(start,finish,length=6)
christmasEve = as.Date("2014-12-24")
print(dates[2])
print(christmasEve)
print(class(dates[2]))
print(class(christmasEve))
(christmasEve==dates[2])
output:
[1] "2014-12-24"
[1] "2014-12-24"
[1] "Date"
[1] "Date"
[1] FALSE
Any help would be greatly appreciated!
-Paul
The problem is that you are dividing a number of days that is not a multiple of six by six. Check out:
as.numeric(dates)
# [1] 16435.0 16428.4 16421.8 16415.2 16408.6 16402.0
start - finish
# Time difference of 33 days
Since you are creating the dates as a sequence the dates are not exact round numbers.
> as.numeric(dates)
[1] 16435.0 16428.4 16421.8 16415.2 16408.6 16402.0
> as.numeric(christmasEve)
[1] 16428
> as.character(christmasEve) == as.character(dates[2])
[1] TRUE
It is not possible to test your code as there is no sampleRate. I assumed that sampleRate is 6. You could compare your dates with the code below:
all(as.character(christmasEve) == as.character(dates[2]))
The whole things should work like that
> sampleRate <- 6
>
> start=as.Date("2014-12-31")
> finish=as.Date("2014-11-28")
> dates = seq(start,finish,length=sampleRate)
> christmasEve = as.Date("2014-12-24")
> print(dates[2])
[1] "2014-12-24"
> print(christmasEve)
[1] "2014-12-24"
> print(class(dates[2]))
[1] "Date"
> print(class(christmasEve))
[1] "Date"
> (christmasEve==dates[2])
[1] FALSE
>
> all(christmasEve == dates[2])
[1] FALSE
> all(as.character(christmasEve) == as.character(dates[2])
+ )
[1] TRUE
I have these two characters and the "as.numeric" function doesn't work same for them. Can anyone help me why this is happening?
options(digits=22)
a="27"
as.numeric(a)
[1] 27.00000000000000000000
a="193381411288395777"
as.numeric(a)
[1] 193381411288395776.0000
It can be seen that in the second case the last digit is not "7" and it is "6". Basically the "as.numeric" function decreases 1 unit from the number in the second case.
Any help is appreciated.
You need to learn about the limits of representation of exact numbers. R can tell you what it has:
R> .Machine
$double.eps
[1] 2.22045e-16
$double.neg.eps
[1] 1.11022e-16
$double.xmin
[1] 2.22507e-308
$double.xmax
[1] 1.79769e+308
$double.base
[1] 2
$double.digits
[1] 53
$double.rounding
[1] 5
$double.guard
[1] 0
$double.ulp.digits
[1] -52
$double.neg.ulp.digits
[1] -53
$double.exponent
[1] 11
$double.min.exp
[1] -1022
$double.max.exp
[1] 1024
$integer.max
[1] 2147483647
$sizeof.long
[1] 8
$sizeof.longlong
[1] 8
$sizeof.longdouble
[1] 16
$sizeof.pointer
[1] 8
R>
Use the int64 package:
library(int64)
> as.int64("193381411288395777")
[1] 193381411288395777
I'm trying to set a time value into a data frame:
ps = data.frame(t(rep(NA, 2)))
ps[1,1] = strptime('10:30:00', '%H:%M:%S')
but I get the error:
provided 9 variables to replace 1 variables
since a time value is a list (?) in R it thinks I'm trying to set 9 columns, when I really just want to set the one column to that class.
What can I do to make this set properly?
This is due to the result of strptime() being an object of class "POSIXlt":
> ps = data.frame(t(rep(NA, 2)))
> ps[1,1] = strptime('10:30:00', '%H:%M:%S')
Warning message:
In `[<-.data.frame`(`*tmp*`, 1, 1, value = list(sec = 0, min = 30L, :
provided 9 variables to replace 1 variables
> strptime('10:30:00', '%H:%M:%S')
[1] "2012-03-21 10:30:00"
> class(strptime('10:30:00', '%H:%M:%S'))
[1] "POSIXlt" "POSIXt"
A "POSIXlt" object is a list representation (hence the lt rather than the ct in the class name) of the time:
> foo <- strptime('10:30:00', '%H:%M:%S')
> str(foo)
POSIXlt[1:1], format: "2012-03-21 10:30:00"
> unclass(foo)
$sec
[1] 0
$min
[1] 30
$hour
[1] 10
$mday
[1] 21
$mon
[1] 2
$year
[1] 112
$wday
[1] 3
$yday
[1] 80
$isdst
[1] 0
A "POSIXlt" object is a list of length 9:
> length(unclass(foo))
[1] 9
hence the warning message, as the object is being stripped back to it's constituent parts/representation. You can stick the "POSIXct" representation in instead without generating the warning:
> ps[1,1] = as.POSIXct(strptime('10:30:00', '%H:%M:%S'))
> ps[1,1]
[1] 1332325800
but we are still loosing the class information. Still, you can go back to the "POSIXct" representation later using the as.POSIXct() function but you will need to specify the origin argument. See ?POSIXct for more.
> class(ps[1,1])
[1] "numeric"
A solution is to coerce ps$X1 to be class "POSIXct" before inserting the time:
> ps = data.frame(t(rep(NA, 2)))
> ps <- transform(ps, X1 = as.POSIXct(X1))
> ps[1,1] <- as.POSIXct(strptime('10:30:00', '%H:%M:%S'))
> ps
X1 X2
1 2012-03-21 10:30:00 NA
> str(ps)
'data.frame': 1 obs. of 2 variables:
$ X1: POSIXct, format: "2012-03-21 10:30:00"
$ X2: logi NA
No warning (as before with as.POSIXct()) but also the class information is retained, where before it was lost. Do read ?`[.data.frame`, especially the Coercion section which has some details; but my take how is that understanding the coercion in replacements like this is tricky.