R語言中判斷是否是整數。以及讀寫excel


今天接手一個重復性工作, 需要手工把產品運營們在excel里寫的活動規則, 插入數據庫表中。為了減少出錯, 提高效率。 再加上最近剛剛學R語言, 就用R練練手, 自動生成mysql的sql語句。 一次性提交。

剛才就是判斷一個值是否是整數折騰了一小會兒。后來發現R判斷這個很簡單。

就是as.integer(money) != as.numeric(money)  

require("XLConnect")
library("WriteXLS")
wb <-loadWorkbook("activity.xls",create=FALSE)
df1 <- readWorksheet(wb,sheet="work")

for(i in 2:length(df1$activityID) )
{
activityID <- df1$activityID[i]
money = df1$money[i];
if(as.integer(money) != as.numeric(money))
{
rules <- sprintf("'rules/taxiRebateBySameCashAmount/rebateRules%3.2fyuan.xls'", as.numeric(money))
}else
{
rules <- sprintf("'rules/taxiRebateBySameCashAmount/rebateRules%dyuan.xls'", as.integer(money))
}
df1$sql[i]=sprintf("INSERT INTO t_activity_rule (activity_id, rule_type, rule_location) SELECT %d, 1, %s FROM dual WHERE not exists (select * from t_activity_rule where t_activity_rule.activity_id = %d and t_activity_rule.rule_type=1); update t_activity_rule set rule_location = %s where t_activity_rule.activity_id = %d and t_activity_rule.rule_type=1;"
, activityID, rules, activityID,rules ,activityID)

}
WriteXLS(df1,ExcelFileName = "activity.xls", SheetNames = "work")

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM