Keep Before and After Date of an External List

Keep before and after date of an external list

One approach could be to expand dframe1 dataset and include rows with has +1 and -1 date for each id and name. We remove the original rows of dframe1 and do an inner_join with dframe2.

library(dplyr)

dframe1 %>%
mutate(date = as.Date(date), date1 = date) %>%
group_by(id, name) %>%
tidyr::complete(date1 = seq(date1 - 1, date1 + 1, by = "1 day")) %>%
filter(date1 != date | is.na(date)) %>%
select(-date) %>%
rename(date = 3) %>%
inner_join(dframe2 %>% mutate(date = as.Date(date)))

#Joining, by = c("id", "name", "date")
# A tibble: 10 x 4
# Groups: id, name [5]
# id name date text_sth
# <int> <chr> <date> <chr>
# 1 1 Amazon 2008-11-05 text here
# 2 1 Google 2008-10-31 another text
# 3 1 Google 2008-11-02 test
# 4 1 Google 2008-11-02 another text
# 5 1 Yahoo 2008-10-31 other
# 6 1 Yahoo 2008-11-02 text_sth
# 7 2 Amazon 2008-10-31 etc
# 8 2 Amazon 2008-11-02 another text
# 9 2 Google 2008-11-01 test
#10 2 Google 2008-11-03 text here

To add a new columns we can add another mutate statement.

dframe1 %>%
mutate(date = as.Date(date), date1 = date) %>%
group_by(id, name) %>%
tidyr::complete(date1 = seq(date1 - 1, date1 + 1, by = "1 day")) %>%
filter(date1 != date | is.na(date)) %>%
select(-date) %>%
mutate(col = c("before", "after")) %>%
rename(date = 3) %>%
inner_join(dframe2 %>% mutate(date = as.Date(date)))

Keep values from a list based on the first timestamp record

An option using base R

dframe$date <- as.Date(dframe$date)
aggregate(date~ ., dframe, min)
# id name date
#1 1 Amazon 2008-11-04
#2 2 Amazon 2008-11-01
#3 1 Google 2008-11-01
#4 2 Google 2008-11-02
#5 1 Yahoo 2008-11-01

Keep rows before a date using ids

We can do a left_join and then filter

library(dplyr)
library(lubridate)
left_join(df1, df2, by = 'id') %>%
filter(ymd_hms(date.x) <= ymd_hms(date.y)) %>%
select(id, date = date.x, values)
#id date values
#1 1 2019/12/11 20:30:12 23
#2 1 2019/12/12 09:20:12 4
#3 2 2019/12/11 12:20:12 4
#4 2 2019/12/11 19:20:12 2

Select columns that contain only values from an external list

You can use the following solution:

library(dplyr)

x %>%
select(where(function(x) all(x %in% c(1:5, NA))))

col3 col4
1 1 1
2 2 2
3 3 3
4 1 4
5 2 5
6 NA NA

Or using a formula:

x %>%
select(where(~ all(.x %in% c(1:5, NA))))

Since the discussion just heated up on this, in case you would like to know how R interprets formulas created by ~ pronounced twiddle, just wrap it inside purrr::as_mapper. This is a function R calls behind the scene when you use this syntax for an anonymous function:

as_mapper(~ all(.x %in% c(1:5, NA)))

<lambda>
function (..., .x = ..1, .y = ..2, . = ..1)
all(.x %in% c(1:5, NA))
attr(,"class")
[1] "rlang_lambda_function" "function"

Here .x argument is equivalent to the first argument of our anonymous function.

How to I modify an external list?

You can use json, pickle, or any similar library to write data to a file and read it back and store in a list. This will create the file save.json in your current working directory that contains json-information about your list. You will notice that your list will always contain the numbers you added before, it doesn't "forget" what you added to the list since it's saved in a file.

import os
import json

foo = [1,2,5,9] #default

#if safe file exists, load its contents into foo list
if os.path.isfile("save.json"):
with open("save.json", "r") as file:
foo = json.loads(file.read())

foo.append(int(input("Enter a number to add to foo")))
print(foo)

#write foo list to file
with open("save.json", "w") as file:
file.write(json.dumps(foo))

How do I save a value in an external variable?

From googling tk.Label it looks like the idea of textvariable is that it refers to a mutable tk.StringVar, not a normal Python str (which is immutable). Hence all you need to do is declare the StringVar in the outer scope and then update it inside the callback:

    date = tk.StringVar()
def set_date():
date.set(cal.get_date())

ttk.Button(top, text="Aceptar", command=set_date).pack()
labelFecha = Label(root, textvariable=date)


Related Topics



Leave a reply



Submit