如何在R中做Countifs

时间:2019-03-27 08:32:44

标签: r data.table countif

数据:

         public class CustomizedExceptionHandler implements UncaughtExceptionHandler {

    private UncaughtExceptionHandler defaultUEH;
    private String localPath;
    public CustomizedExceptionHandler(String localPath) {
        this.localPath = localPath;
        //Getting the the default exception handler
        //that's executed when uncaught exception terminates a thread
        this.defaultUEH = Thread.getDefaultUncaughtExceptionHandler();
    }

    public void uncaughtException(Thread t, Throwable e) {

        //Write a printable representation of this Throwable
        //The StringWriter gives the lock used to synchronize access to this writer.
        final Writer stringBuffSync = new StringWriter();
        final PrintWriter printWriter = new PrintWriter(stringBuffSync);
        e.printStackTrace(printWriter);
        String stacktrace = stringBuffSync.toString();    
        printWriter.close();

        if (localPath != null) {
            writeToFile(stacktrace);
        }

        defaultUEH.uncaughtException(t, e);
    }

    private void writeToFile(String currentStacktrace) {
        try {

            //Gets the Android external storage directory & Create new folder Crash_Reports
            File dir = new File(Environment.getExternalStorageDirectory(),
                    "Crash_Reports");
            if (!dir.exists()) {
                dir.mkdirs();
            }

            SimpleDateFormat dateFormat = new SimpleDateFormat(
                    "yyyy_MM_dd_HH_mm_ss");
            Date date = new Date();
            String filename = dateFormat.format(date) +".PAkultie";

            // Write the file into the folder
            File reportFile = new File(dir, filename);
            FileWriter fileWriter = new FileWriter(reportFile);
            fileWriter.append(currentStacktrace);
            fileWriter.flush();
            fileWriter.close();
        } catch (Exception e) {
            Log.e("ExceptionHandler", e.getMessage());
        }
    }

求和,如果数据在范围内,则可以使用(from my previous question):

set.seed(42)
df1 = data.frame(
  Date = seq.Date(as.Date("2018-01-01"),as.Date("2018-01-30"),1),
  value = sample(1:30),
  Y = sample(c("yes", "no"), 30, replace = TRUE)
)

df2 = data.frame(
  Date = seq.Date(as.Date("2018-01-01"),as.Date("2018-01-30"),7)
)

我该如何计数?

因为我尝试

library(data.table)

df1$start <- df1$Date
df1$end <- df1$Date

df2$start <- df2$Date
df2$end <- df2$Date + 6

setDT(df1, key = c("start", "end"))
setDT(df2, key = c("start", "end"))

d = foverlaps(df1, df2)[, list(mySum = sum(value)), by = Date ]

我收到错误

  

没有适用于“组”的适用于“ c('double','numeric')”类对象的方法

4 个答案:

答案 0 :(得分:3)

我们可以使用.N

foverlaps(df1, df2)[, list(myCount = .N), by = Date ]
#          Date myCount
# 1: 2018-01-01       7
# 2: 2018-01-08       7
# 3: 2018-01-15       7
# 4: 2018-01-22       7
# 5: 2018-01-29       2

答案 1 :(得分:2)

cookie.setDomain ("abc.stackoverflow.com");
cookie.setPath("/service/test");

答案 2 :(得分:2)

如果要计算每个日期的行数,可以尝试.N

foverlaps(df1, df2)[, .(mysum = .N), by = Date ]
         Date mysum
1: 2018-01-01     7
2: 2018-01-08     7
3: 2018-01-15     7
4: 2018-01-22     7
5: 2018-01-29     2

如果您要计算每个日期的唯一值,可以尝试uniqueN()

foverlaps(df1, df2)[, .(mysum = uniqueN(value)), by = Date ]
         Date mysum
1: 2018-01-01     7
2: 2018-01-08     7
3: 2018-01-15     7
4: 2018-01-22     7
5: 2018-01-29     2

.NuniqueN()都来自{data.table}

答案 3 :(得分:1)

尝试使用list(mySum = count(value))而不是c(mySum = count(value))。该代码然后为我运行。

d2 <-  foverlaps(df1, df2)[, c(mySum = count(value)), by = Date ]