'Creating aggregated tables with calculations using loops in R
Wondering how I can create a table with the results of different calculations in R. Here's an example using mtcars df
# Load tidyverse
library(tidyverse)
# Select only cyl vs and am from mtcars for simplicity
mt_select <- select(mtcars, c(cyl, vs, am))
# Here is the table I wish to make using some type of looping function
# (I have like 40 variables in actual dataset)
freq_table <- mt1 %>% group_by(cyl) %>%
summarise(n = n(),
vs_sum = sum(vs),
vs_perc = sum(vs)/n*100,
am_sum = sum(am),
am_perc = sum(am)/n*100)
print(freq_table)
Here is my attempt. Main issues I can't figure out:
- Includes sums for "0" answers,
- I don't know how to add a percent column
- Don't know how to merge these all into one table
# make a vector to loop through
mt_vars <- names(mt_select)
# Loop to make tables
for (i in mt_vars){
mt_select %>%
group_by(cyl) %>%
count_(i) %>%
print()
}
Been trying to figure out how to make this for months and always just decide I don't need the table, or something else. Any help is greatly appreciated!!
Solution 1:[1]
connect has implicit Qt::ConnectionType parameter. It will be defined as Qt::QueuedConnection when sender and receiver are in different threads. You can use QObject::moveToThread to move worker to another thread.
If for some reason you want use QtConcurrent::run here - you can use it inside func1 implementation.
Check both versions:
class Worker : public QObject {
public:
void DoSlowCalculation() {
qDebug() << "Worker::DoSlowCalculation thread:" << QThread::currentThreadId();
QThread::msleep(2000); // 2 sec
qDebug() << "Worker::DoSlowCalculation finished";
}
};
class ConcurrentExecutor : public QObject {
public:
void DoSlowCalculation() {
qDebug() << "ConcurrentExecutor::DoSlowCalculation thread:" << QThread::currentThreadId();
QThread::msleep(2000); // 2 sec
qDebug() << "ConcurrentExecutor::DoSlowCalculation finished";
}
void DoSlowCalculationConcurrent() {
QtConcurrent::run(this, &ConcurrentExecutor::DoSlowCalculation);
}
};
int main(int argc, char** argv) {
QApplication app(argc, argv);
qDebug() << "Main thread:" << QThread::currentThreadId();
QThread worker_thread;
Worker worker;
worker.moveToThread(&worker_thread);
worker_thread.start();
QPushButton btn;
btn.setText("Click to perform slow calculation");
QObject::connect(&btn, &QAbstractButton::clicked, &worker,
&Worker::DoSlowCalculation);
ConcurrentExecutor concurr_executor;
QObject::connect(&btn, &QAbstractButton::clicked, &concurr_executor,
&ConcurrentExecutor::DoSlowCalculationConcurrent);
worker_thread.start();
QObject::connect(&app, &QCoreApplication::aboutToQuit, &worker_thread,
&QThread::quit);
btn.show();
return app.exec();
}
P.S. Strongly suggest to stop using SIGNAL and SLOT macros, that's Qt4 way to connect. More info on this topic here.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | jdfa |
