Use Endpoints Function to Get Start Points Instead

Use endpoints function to get start points instead?

You can create a startpoints function like this

startpoints <- function (x, on = "months", k = 1) {
head(endpoints(x, on, k) + 1, -1)
}

Understanding function endpoints

Let's prepare sample data :

month <- seq.Date(from=Sys.Date()-5,to=Sys.Date()+10,by="day")

# [1] "2018-06-18" "2018-06-19" "2018-06-20" "2018-06-21" "2018-06-22" "2018-06-23" "2018-06-24" "2018-06-25" "2018-06-26"
# [10] "2018-06-27" "2018-06-28" "2018-06-29" "2018-06-30" "2018-07-01" "2018-07-02" "2018-07-03"

xts::endpoints gives the index of the last observation of each month, always beginning with 0:

library(xts)
endpoints(month, "months")
# [1] 0 13 16

So if you add 1, you'll have the index of the first available day of the next month and conveniently, the 0 will be the index of the 1st day of the 1st month:

endpoints(month, "months") + 1
# [1] 1 14 17

The last value is meaningless though, so we drop it:

head(endpoints(month, "months") + 1, -1)
# [1] 1 14

And we end up with your solution:

first.values <- month[head(endpoints(month, "months") + 1, -1)]
# [1] "2018-06-18" "2018-07-01"

An alternate way of doing it:

month <- as.xts(month)
first_as_list <- lapply(split(month,f="month"), function(x) index(x)[1])
do.call(c,first_as_list)
# [1] "2018-06-18" "2018-07-01"

How to update my dictionary to hold both Start and Endpoints

First of all your expected output is not a valid dictionary. I assume however you want to generate:

d = {'a' : 
{ 'E' : (x,y) , # EndPoint
'S' : (x,y) } # StartPoint
}

In that case you can simply write:

from collections import defaultdict

points = defaultdict(dict)
for i,stati in enumerate(state):
for j,statij in enumerate(stati):
if statij.isalpha():
low_statij = statij.lower()
if statij.isupper():
points[low_statij]['S'] = (i,j)
else:
points[low_statij]['E'] = (i,j)

This generates:

>>> points
defaultdict(<class 'dict'>, {'b': {'E': (2, 1), 'S': (1, 10)}, 'a': {'E': (0, 0), 'S': (4, 4)}, 'c': {'E': (3, 2), 'S': (0, 4)}})

In case you want to convert it back to a vanilla dictionary, you can call dict on points:

>>> dict(points)
{'b': {'E': (2, 1), 'S': (1, 10)}, 'a': {'E': (0, 0), 'S': (4, 4)}, 'c': {'E': (3, 2), 'S': (0, 4)}}

C# Reference: Line updates its EndPoint due to changed from different Line instance

In both SetLength() and SetAngle(), you're creating a NEW Point instance to update your Line. This means that particular Line instance will reference the newly created Point, but all the other Line instances will still reference the originally created Point.

Simply change:

EndPoint = new Point(x2, y2);

To:

EndPoint.X = x2;
EndPoint.Y = y2;

Make sure to do this in both of your functions!

Getting data from minute xts objects at particular minute

Time of day subsetting currently only works with a range. You could use UpRatio["T10:14:59.999/T10:15:00"] instead of last(UpRatio["T10:14:00/T10:15:00"]). Also note that this will return the 10:15 time for every day in your object.



Related Topics



Leave a reply



Submit