Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tidy? #110

Closed
PMassicotte opened this issue Aug 3, 2016 · 7 comments
Closed

Tidy? #110

PMassicotte opened this issue Aug 3, 2016 · 7 comments

Comments

@PMassicotte
Copy link
Owner

Should we reformat the output as this?

library(gtrendsR)

res <- gtrends("nhl", geo = "CA")

res <- res$trend
head(res)

##        start        end nhl.CA
## 1 2004-01-04 2004-01-10     21
## 2 2004-01-11 2004-01-17     21
## 3 2004-01-18 2004-01-24     22
## 4 2004-01-25 2004-01-31     20
## 5 2004-02-01 2004-02-07     21
## 6 2004-02-08 2004-02-14     21

res2 <- data.frame(start = res$start, 
                   end = res$end, 
                   keyword = "nhl",
                   geo = "CA", 
                   hits = res$nhl.CA)

head(res2)

##        start        end keyword geo hits
## 1 2004-01-04 2004-01-10     nhl  CA   21
## 2 2004-01-11 2004-01-17     nhl  CA   21
## 3 2004-01-18 2004-01-24     nhl  CA   22
## 4 2004-01-25 2004-01-31     nhl  CA   20
## 5 2004-02-01 2004-02-07     nhl  CA   21
## 6 2004-02-08 2004-02-14     nhl  CA   21
@eddelbuettel
Copy link
Collaborator

eddelbuettel commented Aug 3, 2016

Oh, gosh, I had to squint hard to get at what you wanted here as

R> library(gtrendsR)
Creating a generic function fortoJSONfrom packagejsonlitein packagegoogleVisR> res <- gtrends("nhl", geo = "CA")
R> head(res$trend)
       start        end nhl.CA
1 2004-01-04 2004-01-10     21
2 2004-01-11 2004-01-17     21
3 2004-01-18 2004-01-24     22
4 2004-01-25 2004-01-31     20
5 2004-02-01 2004-02-07     21
6 2004-02-08 2004-02-14     21
R> class(res$trend)
[1] "data.frame"

wasn't quite sure what you suggested. In short, "could do" -- but what about more complex queries like the recent 'cloud and big data'. At some point it gets silly to repeat this. A more compact form would be to set an attribute via attr() .... but then all this is already in the res object returned.

So I don't really see yet why we need to stick it into the data.frame. Maybe add as an example, or maybe add a helper function resToTidy() ?

@PMassicotte
Copy link
Owner Author

The only thing that "bug" me is nhl.CA where we have both keyword and country in the column name.

BTW, just google made some changes and we can now download CSV by chuck.

https://www.google.com/trends/explore?date=all&q=nhl

@eddelbuettel
Copy link
Collaborator

Gotcha. In that case ... shall we just override that with maybe a fixed word count, or hits, or put the (possibly long multi-word) query there?

Download by chunk ... is better for really large result sets?

@PMassicotte
Copy link
Owner Author

Alright, I will work on that (the tidy thing).

I will make more sens for request list this:

> head(gtrends("蘋果",geo="TW")$trend)
       start        end X.e8..98..8b..e6..9e..9c..TW
1 2004-01-04 2004-01-10                           20
2 2004-01-11 2004-01-17                           21
3 2004-01-18 2004-01-24                           22
4 2004-01-25 2004-01-31                           17
5 2004-02-01 2004-02-07                           19
6 2004-02-08 2004-02-14                           26

@eddelbuettel
Copy link
Collaborator

Absolutely! I like 'hits' as a column number, and a 'long-to-wide' exporter helper may be useful.

But this grumpy old man doesn't fall for every seasonal flavour of the tidyverse so just get off my lawn...

@PMassicotte
Copy link
Owner Author

PMassicotte commented Aug 4, 2016

Hehehe do not fear my friend, I did it like this:

  trend <- reshape(
    trend,
    varying = tmp_kw,
    v.names = "hits",
    direction = "long",
    timevar = "keyword",
    times = requested_kw
  )

@eddelbuettel
Copy link
Collaborator

No school like the old school. Using stats::reshape is the sign of a true craftsman :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants