[R] sqlFetch and factor conversion

Hosack, Michael mhosack at state.pa.us
Tue Jun 14 21:18:53 CEST 2011


R users,

Hello, I used sqlFetch to import a data table from MS Access 2007 and I set as.is = TRUE to 
prevent character vectors from becoming factors. However, I can't figure out how to prevent
the $ operator from being added by sqlFetch to the end of each character vector(e.g. TD$ and SITE$). 
This causes subsequent parts of my code not to work and also results in some behavior that I don't 
understand. For example, 

DF$TOD <- with(DF, ifelse(TD == "AFTERNOON", 2, 1))

does not work, however DF$TD outputs the TD vector, whereas DF$TD$ outputs nothing.
Furthermore, DF[ , 'TD$'] provides the same vector as does DF$TD, whereasDF[ , 'TD'] gives an 
error ("undefined columns selected"). I don't understand why TD is valid here (DF$TD) but not 
here (DF[ , 'TD']). My goal is to import the Access data table as a data frame with character vectors 
where factors would otherwise be and the column names should remain unchanged. I was hoping using 
the RODBC package would make things easier by not having to edit intermediate files whenever I 
edited my databases.

Thank you,

Mike

  
DF <-
structure(list(ID = 1:20, MM = c(5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 
5, 5, 5, 5, 5, 5, 5, 5, 5, 5), DD = c(2, 2, 2, 2, 3, 3, 3, 3, 
5, 5, 5, 5, 9, 9, 9, 9, 10, 10, 10, 10), YY = c(2010, 2010, 2010, 
2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 
2010, 2010, 2010, 2010, 2010, 2010), `TD$` = c("AFTERNOON", "AFTERNOON", 
"AFTERNOON", "AFTERNOON", "AFTERNOON", "AFTERNOON", "AFTERNOON", 
"AFTERNOON", "AFTERNOON", "AFTERNOON", "AFTERNOON", "AFTERNOON", 
"AFTERNOON", "AFTERNOON", "AFTERNOON", "AFTERNOON", "MORNING", 
"MORNING", "MORNING", "MORNING"), WEEK = c(1, 1, 1, 1, 1, 1, 
1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2), SITENUM = c(102, 103, 
104, 101, 102, 103, 104, 101, 102, 103, 104, 101, 104, 101, 102, 
103, 103, 104, 101, 102), `SITE$` = c("Lampe", "East Avenue", 
"North East", "Walnut Creek", "Lampe", "East Avenue", "North East", 
"Walnut Creek", "Lampe", "East Avenue", "North East", "Walnut Creek", 
"North East", "Walnut Creek", "Lampe", "East Avenue", "East Avenue", 
"North East", "Walnut Creek", "Lampe"), ARRIVE = c(1430, 1540, 
1715, 1945, 1430, 1540, 1715, 1945, 1430, 1540, 1715, 1945, 1430, 
1700, 1910, 2020, 700, 835, 1105, 1315), WAIT = c(60, 60, 100, 
100, 60, 60, 100, 100, 60, 60, 100, 100, 100, 100, 60, 60, 60, 
100, 100, 60), DEPART = c(1530, 1640, 1855, 2125, 1530, 1640, 
1855, 2125, 1530, 1640, 1855, 2125, 1610, 1840, 2010, 2120, 800, 
1015, 1245, 1415), LAUNANG = c(0L, 0L, 0L, 0L, 0L, 2L, 1L, 3L, 
1L, 0L, 2L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 1L), LAUNNON = c(0L, 
0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 1L), LANDANG = c(0L, 0L, 3L, 0L, 1L, 0L, 2L, 0L, 0L, 
0L, 4L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L), LAUNUNK = c(0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 1L), LANDNON = c(1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), LANDUNK = c(0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 1L)), .Names = c("ID", "MM", "DD", "YY", "TD$", "WEEK", 
"SITENUM", "SITE$", "ARRIVE", "WAIT", "DEPART", "LAUNANG", "LAUNNON", 
"LANDANG", "LAUNUNK", "LANDNON", "LANDUNK"), row.names = c(NA, 
20L), class = "data.frame")



More information about the R-help mailing list