0

Tried googling and youtube but still stuck, so here it goes:

I download a df from an API and since I want my update to always get the latest yr and month I don't want to specify that in my list that I send to the API. This means that I get my results for all yrs and months. -> Since I'm only interested in the latest entry (e.g. latest month) I need some function to identify which is the latest date in the df and then get rid of all data that is older than that.

I've converted the column, called "månad" to Date (format: YYYY-MM-DD), but what's next?

CODE

library(pxweb)
library(tidyverse)
library(lubridate)

# PXWEB query - create list
pxweb_query_list_BAS <- 
  list("Region"=c("22"),
       "Kon" =c("1","2"),
       "Alder" =c( "20-24",
                   "25-29",
                   "30-34",
                   "35-39",
                   "40-44",
                   "45-49",
                   "50-54",
                   "55-59",
                   "060-64"),
       "Fodelseregion" = c("in","ut"),
       "ContentsCode"=c("000006IJ"))
       #"Tid" = "2022M09") 

# Download data using created list
px_data_BAS <- 
  pxweb_get(url = "https://api.scb.se/OV0104/v1/doris/sv/ssd/START/AM/AM0210/AM0210A/ArbStatusM",
            query = pxweb_query_list_BAS)


# Convert list to df 
df_arbetskraftd <- as.data.frame(px_data_BAS, column.name.type = "text", variable.value.type = "text")

#Convert class of col. månad from chr (string) to date
df_arbetskraftd$månad <- ym(df_arbetskraftd$månad)

DF - overview

enter image description here

If I can get a value of latest date in that format I guess I can figure out how to create the new df in some way (right now I thinking creating a new df with that date and then merging, maybe not the smartest) - but feel free to suggest solution here too.

Thanks!

Christian
  • 117
  • 1
  • 3
  • 10
  • 3
    `subset(mtcars, cyl == max(cyl))` works to remove all rows where `cyl` does not equal the max of it, this also works with `Date`-class objects. If you need more help than that, two things: (1) please do not give just images of data, see https://meta.stackoverflow.com/a/285557 (and https://xkcd.com/2116/); and (2) your question is way more complex than it needs to be: your question has nothing to do with `pxweb`, `plotly`, or the like ... it's _just_ about data in a frame, regardless of how you got the data. Please remove unneeded code. – r2evans Feb 13 '23 at 15:02
  • 1
    A better way to share data (instead of an image) is to use `dput(x)` where `x` is enough but not too much (almost certainly not all), perhaps `dput(head(x,20))` or similar. Feel free to remove columns that are immaterial. See https://stackoverflow.com/q/5963269, [mcve], and https://stackoverflow.com/tags/r/info for other options, though I think `dput` is going to be best for your use here. – r2evans Feb 13 '23 at 15:04
  • Thanks subset did the charm! Sure I will not include img, I though it was helpful for a overview, but you're right not necessary. Also removed unneeded packages, point in including the code was if one wanted to run it - but as you have concluded the question could just be stated in more meta-way. – Christian Feb 13 '23 at 15:19
  • 1
    Images are not wrong in and of themselves, but we can't use them, that's why I said "just images of data", sometimes an image may be justified when provided alongside usable data (such as from `dput(.)`). (I haven't found many times when an image added much that a well-varied sample did not, to be candid, but it's not the _presence_ of an image that is frustrating, it's the _absence_ of something we can use. I prefer to not transcribe data that somebody has freely available and there are easy mechanisms to share.) Thanks! – r2evans Feb 13 '23 at 15:31

0 Answers0