SQL Equivalents in R

The R code has the same effect as the SQL JOIN.> head(CO2)Plant Type Treatment conc uptake1 Qn1 Quebec nonchilled 95 16.02 Qn1 Quebec nonchilled 175 30.43 Qn1 Quebec nonchilled 250 34.84 Qn1 Quebec nonchilled 350 37.25 Qn1 Quebec nonchilled 500 35.36 Qn1 Quebec nonchilled 675 39.2> stateprov <- c(“Mississippi”, “California”, “Victoria”, “New South Wales”, “Quebec”, “Ontario”)> country <- c(“United States”, “United States”, “Australia”, “Australia”, “Canada”, “Canada”)> geo_map <- data.frame(country=country, stateprov=stateprov)> geo_mapcountry Type1 United States Mississippi2 United States California3 Australia Victoria4 Australia New South Wales5 Canada Quebec6 Canada Ontario> colnames(geo_map) <- c(“country”, “Type”)> joinCO2 <- merge(CO2, geo_map, by=c(“Type”))> head(joinCO2)Type Plant Treatment conc uptake country1 Mississippi Mn1 nonchilled 95 10.6 United States2 Mississippi Mn1 nonchilled 175 19.2 United States3 Mississippi Mn1 nonchilled 250 26.2 United States4 Mississippi Mn1 nonchilled 350 30.0 United States5 Mississippi Mn1 nonchilled 500 30.9 United States6 Mississippi Mn1 nonchilled 675 32.4 United StatesIf you prefer to stick with your knowledge of SQL, you can access R data frames directly with standard SQL queries through the use of the sqldf package.Read more data science articles on OpenDataScience.com. More details

Leave a Reply