我正在尝试使用dbplyr软件包计算R中两个日期之间的月份差,我想使用mysql中的“ timestampdiff”本机函数发送sql查询来计算它,但出现错误:
library(tidyverse)
library(lubridate)
library(dbplyr)
db_df <- tbl(con, "creditos")
db_df %>% mutate(diff_month = timestampdiff(month, column_date_1, column_date_2))
但是参数month
的翻译不正确,因为它看起来像R中的对象或函数:
UseMethod(“ escape”)中的错误: 没有适用于“功能”类对象的“转义”适用方法
如果用这种方式写:
db_df %>% mutate(diff_month = timestampdiff("month", column_date_1, column_date_2))
我也会收到一个错误:
您的SQL语法有错误;请查看与您的MySQL服务器版本相对应的手册,以在
附近使用正确的语法month
,column_date_1
,column_date_2
)ASdiff_month
我相信这是因为dbplyr正在将带有双引号的“ month”写入mysql,并且应该没有双引号,例如:
TIMESTAMPDIFF(month, column_date_1, column_date_2) AS `diff_month`
还是有更好的方法使用dbplyr计算月份差异?
答案 0 :(得分:0)
package example.callerid;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.sip.*;
import android.os.Build;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.NotificationManagerCompat;
import android.util.Log;
import android.widget.Toast;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Random;
/**
* Listens for incoming SIP calls, intercepts and hands them off to CallerIDactivity.
*/
public class IncomingCallReceiver extends BroadcastReceiver {
public Calendar calendar;
public SimpleDateFormat dateFormat;
public String date;
public SipManager mSipManager;
public SipSession mSipSesion;
public SipProfile peerProfile;
static private int notifyNum = 1;
/**
* Processes the incoming call, answers it, and hands it over to the
* CallerIDactivity.
* @param context The context under which the receiver is running.
* @param intent The intent being received.
*/
@Override
public void onReceive(Context context, Intent intent) {
mSipManager = SipManager.newInstance(context);
try {
mSipSesion = mSipManager.getSessionFor(intent);
} catch (SipException e) {
e.printStackTrace();
}
peerProfile = mSipSesion.getPeerProfile();
calendar = Calendar.getInstance();
dateFormat = new SimpleDateFormat("dd/MM/yyyy");
date = dateFormat.format(calendar.getTime());
Log.d("CallerID", "OnReceive");
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context, "CHANNEL_ID")
.setSmallIcon(R.mipmap.ic_launcher_round)
.setContentTitle("CallerID" + " " + date)
.setContentText(peerProfile.getDisplayName())
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setAutoCancel(true);
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
if(notifyNum <= 9) {
notificationManager.notify(notifyNum++, mBuilder.build());
}else {notificationManager.notify(notifyNum, mBuilder.build());
notifyNum = 1;
}
int count = 0;
while (count <= 10) {
Toast toast = Toast.makeText(context, peerProfile.getDisplayName(), Toast.LENGTH_SHORT);
toast.show();
count++;
}//end while
}//end onReceive
}//end Class
是month
包中的函数。好像mutate是作为R函数lubridate
而不是文本传递给month
的。
如果您正在使用本机SQL计算时差,则不需要lubridate程序包。
两种可能的解决方案:
month()
,并使用前缀library(lubridate)
引用lubridate
软件包。例如:lubridate::
lubridate::ymd_hms