dc.js pie chart sum example - crossfilter

I have records like this :
{
{"Pass": "10", "Fail": "20", "Untested": "40"}
{"Pass": "20", "Fail": "40", "Untested": "50"}
{"Pass": "30", "Fail": "50", "Untested": "60"}
...
}
Obviously, total = pass + fail + untested. I want to sum up each of these values and then finally show a pie chart using dc.js that shows total Pass, total Fail, total Untested. I couldnt find a good example.
In the example below, each record is converted to a binary loss/gain value. Then the pie chart shows the aggregated loss/gain. I need to aggregate at the collection level, not individual record level. How can I do that ?
Any help is much appreciated.
Ref:
http://nickqizhu.github.io/dc.js/

Take a look at the example in this jsfiddle
The trick is to modify your input data, so that you can create an dimension for all records on the result. So the data should look like this:
[{"result":"Pass","value":"10","_id":0},
{"result":"Fail","value":"20","_id":0},
{"result":"Untested","value":"40","_id":0},
{"result":"Pass","value":"20","_id":1},
{"result":"Fail","value":"40","_id":1},
{"result":"Untested","value":"50","_id":1},
{"result":"Pass","value":"30","_id":2},
{"result":"Fail","value":"50","_id":2},
{"result":"Untested","value":"60","_id":2}]
I did this using a new js library I just created called melt, but it would be fairly simple to duplicate the same logic yourself. Also here is another example in a recent user list discussion.

Related

R: Creating Tidy df based on URL

I want to analyse data from a website regarding visitors. Unfortunately, I'm not sure if I can post the df publicly, so I'll describe it the best I can.
I basically have three columns:
date: containing the date (YYYY-MM-DD),
url: Containing the full url of the page
views: The number of visits for that url for that day
What I want to is to categorize the data based on the url, by making new columns. To take stackoverflow as an example, I have urls like:
stackoverflow.com/questions
stackoverflow.com/job
stackoverflow.com/users
For these I want to create a new categorical variable 'Main_cat' with 'Questions', 'Jobs' and 'Users' as its levels. For that I'm currently using this, which I found in another topic here.
df <- df %>%
mutate(Main_cat= case_when(
grepl(".*flow.com/questions.*", url) ~ "Questions",
grepl(".*flow.com/jobs.*", url) ~ "Jobs",
grepl(".*flow.com/users.*", url) ~ "Users")) %>% mutate(Main_cat = as.factor(Main_cat))
This works decently, though not great. The number of main categories I'm working with is about twelve. My full dataset is about 220.000 observations. So processing in a set-up like this takes a while. Feels like I'm working very inefficient.
In addition I'm working with sub-categories based on countries:
stackoverflow.com/job/belgium
stackoverflow.com/job/brazil
stackoverflow.com/job/china
stackoverflow.com/job/germany
stackoverflow.com/job/france
These I want to divide by new variables like Continent, and Country, since also the countries have subdivisions (...job/belgium/retail, ...job/belgium/it). In the end I would like to sort my data by country, or by sector or both using filter() and then perform an analysis.
I can use the mutate/case_when/grepl for all of the above, but judging from how long it takes R to finish, something doesn't seem right. I'm hoping there's a better way that takes less time to process.
Hope this is clear enough, thanks in advance!

if function to create a new variable from three dummies in R

I need R to create a new categorical variable using three dummies that I have in my dataset. I am trying to use "if" and "else if". I have managed to do it in other sotfware but I can't find the solution in R. Would someone please take a look at my code and tell me what's wrong?
data$newvariable<- if(data$dummyA[1]) {
(data$newvariable<-"1")
} else if(data$dummyB[1]){
(data$newvariable<-"2")
} else if(data$dummyC[1]){
(data$newvariable<-"3") }
I hope not to duplicate questions. Sorry! And thank you in advance!
What I get as a result is all cases categorized as "1", which makes me think there is something missing between the first if statement and the following one. But I can't find the solution, help, please!
THANKS!
You can use ifelse() to do the job.
data$newvariable <- ifelse(data$dummyA[1], "1",
ifelse(data$dummyB[1], "2",
ifelse(data$dummyC[1], "3")))
This should work, but can not test it without a reproducible example.

Removing columns from an index request in a data frame

Hi I currently have the code:
matrixed.data <- data.matrix(df[1:row.dim,7:col.dim])
Where the row.dim and col.dim are variables for the size of the whole frame. I would like to remove the column "df$WEATHER" that is included in the col.dim selection but don't know how to word it. I have tried adding - df$WEATHER and !df$WEATHER inside the bracket but fear I'm misinterpreting the scope of these commands.
Is it possible to do this without creating a new col.dim variable; I'm trying to keep the code as limitless as possible as the data frame may increase in size in the future.
Thank you digEmAll! I thought it would be reasonably simple I'm just a bit too green at R to think of something like that. For others what worked for me was:
(df[1:row.dim, setdiff(7:col.dim,which(names(df) == "WEATHER"))])

R Shiny : how to use several checkboxInput values as combination?

I am new to R, so I figure there is probably an easy way to do this. I searched here on Stack Overflow and couldn’t find a similar question, so I apologize if I missed it.
I have 9 checkboxInputs in my ui.R, numbered from "1" to "9". The values are in character format.
If I check boxes "3" and "9", the value in the table is "39".
If I check boxes "4", "6", "8" and "9", the value to serach in the table is "4689". There are a lot of combinations.
So, I would like to know how can I concatenate all values from the checked boxes, in reactive environment, to search the right combination in a data frame ? I think it's something too complicated for a newbie as me. I have to get more experience.
Thank you very much for setting me on the right path.

How generally to refer to a function's argument in the course of further coding?

How can I refer to the choice of a function's argument in the course of my further coding?---A specific example:
library("quantmod")
INDEX<-get(getSymbols("^GDAXI", from="2006-01-01"))
INDEX.SMA<-SMA(INDEX[,4],n=360)
INDEX<-INDEX[,4]
colnames(INDEX)<-c("Close")
colnames(INDEX.SMA)<-"360"
The function SMA(INDEX,n=360) generates the moving average of closing prices in INDEX.
I would like to have the chosen argument of 360 be automatically reflected in colnames(INDEX.SMA). Thus, I don't want to change it manually to, say, "200", four lines further down after changing my code to
INDEX.SMA<-SMA(INDEX[,4],n=200)
Replacing
colnames(INDEX.SMA)<-"360"
with
colnames(INDEX.SMA)<-as.character(length(INDEX.SMA)-sum(!is.na(INDEX.SMA))+1)
did the job in this specific example. Is there a more general solution?
If you need to use a value in multiple places, you should make it a variable
library("quantmod")
mytime<-360
INDEX<-get(getSymbols("^GDAXI", from="2006-01-01"))
INDEX.SMA<-SMA(INDEX[,4],n=mytime)
INDEX<-INDEX[,4]
colnames(INDEX)<-c("Close")
colnames(INDEX.SMA)<-as.character(mytime)

Resources