带有句点的leftstr和列名出错,使用R中的sqldf

时间:2016-07-20 01:44:32

标签: r string sqldf

以下员工表来自http://www.analyticsvidhya.com/blog/2015/12/sql-commands-common-excel-operations/。该表后面的代码基于Example 8B。

Employee <- structure(list(ECODE = c("A011", "A001", "A007"), DOJ = 
c("3-Jul-12", "12-Jun-12", "13-Aug-12"), Experience = c(2.1, 2.2, 2),
Gender = c("Male", "Male", "Female"), Department = c("Support", "Admin", 
"Support"), No_Of_Hours = c(17.42, 15.45, 13.54), Pay_Per_Hours = c(40L, 
45L, 44L), Total_Payout = c(696.8, 695.25, 595.72), City = c("Delhi", 
"Delhi", "Mumbai")), .Names = c("ECODE", "DOJ", "Experience", "Gender", 
"Department", "No_Of_Hours", "Pay_Per_Hours", "Total_Payout", "City"), 
row.names = c(NA, -3L), class = c("data.table", "data.frame"))

# Employee 
#     ECODE DOJ       Experience Gender  Department    No_Of_Hours 
# 1:  A011  3-Jul-12  2.1        Male    Support       17.42 
      Pay_Per_Hours
      40
# 2:  A001  12-Jun-12 2.2        Male    Admin         15.45            
      45
# 3:  A007  13-Aug-12 2.0        Female  Support       13.54            
      44
#     Total_Payout   City
# 1:  696.80         Delhi
# 2:  695.25         Delhi
# 3:  595.72         Mumbai

在示例8B中使用[LEFT(x,N)],但我发现Leftstr(how to use right/left to split a variable in sqldf, as in left(x,n))是sqldf中完成的方式。

由于列名中没有特殊字符(“。”),因此操作按预期工作。

sqldf("Select *, Leftstr(City,3) as 'City_Code' from Employee where
Department = 'Admin'")

#    ECODE  DOJ         Experience  Gender   Department  No_Of_Hours
# 1  A001   12-Jun-12   2.2         Male     Admin       15.45     
#    Pay_Per_Hours  Total_Payout  City   City_Code
#    45             695.25        Delhi  Del

在下面的示例中,与我的实际数据集类似,列名称中有一个句点,并且不期望答案。

sqldf("Select *, Leftstr('City.1',3) as 'City_Code' from Employee where 
Department = 'Admin'")

#    ECODE  DOJ         Experience  Gender   Department  No_Of_Hours
# 1  A001   12-Jun-12   2.2         Male     Admin       15.45     
#    Pay_Per_Hours  Total_Payout  City   City_Code
#    45             695.25        Delhi  Cit

sqldf操作的列名中有特殊字符时,leftstr代码需要执行哪些操作?

1 个答案:

答案 0 :(得分:1)

单引号环绕常量。要引用列名称,请用双引号或方括号括起来。假设City列的名称为City.1

sqldf("Select *, Leftstr([City.1],3) as 'City_Code' from Employee where 
  Department = 'Admin'")

,并提供:

  ECODE       DOJ Experience Gender Department No_Of_Hours Pay_Per_Hours
1  A001 12-Jun-12        2.2   Male      Admin       15.45            45
  Total_Payout City.1 City_Code
1       695.25  Delhi       Del