r sql
play

R - SQL DBI RSQLite sqldf : sqldf > - PowerPoint PPT Presentation

RSQLite 10 R - SQL SQLite , R SQLite : R -


  1. RSQLite תפשל חותפ דוקב ץופנ שומימ אוה קלח 10 R ב םדקתמ תונכת - SQL SQLite • • ןיבל וניב רשקלו בשחמה לע ןיקתהל ידכ , R SQLite • תוליבחה תא ןיקתנ : R ב - SQL DBI • RSQLite • sqldf • תא ןעטנ : sqldf • > library(sqldf) תוליבחה תורחאה תושורדהתיטמוטוא ונעטיי • קלח 10 • רטסמסב ' עשת " ו ,2015/16 1 2 • קלח 10 • רטסמסב ' עשת " ו ,2015/16 • R ב םדקתמ תונכת - R ב םדקתמ תונכת - RSQLite RSQLite ) סרוקה רתאב ( , ץבוקה תא בשחמל דירונ • םינותנה סיסבב תואלבטה : baseball.db • לובסייב ינקחש לע םינותנ סיסב ליכמש > dbListTables(con) המיאתמה היירפסל • תא ןווכל חוכשל אל [1] "AllstarFull" "Appearances" R [3] "AwardsManagers" "AwardsPlayers" • ל רביירד רוצינ - : SQLite [5] "AwardsShareManagers" "AwardsSharePlayers" > drv <- dbDriver("SQLite") [7] "Batting" "BattingPost" • םינותנה סיסבל רבחתנ : [9] "Fielding" "FieldingOF" [11] "FieldingPost" "HallOfFame" > con <- dbConnect(drv, dbname="baseball.db") [13] "Managers" "ManagersHalf" הדובעה ףוסב יאדכ – • [15] "Master" "Pitching" > dbDisconnect(con) [17] "PitchingPost" "Salaries" > dbUnloadDriver(drv) … 3 ב םדקתמ תונכת - 4 קלח 10 • רטסמסב ' עשת " ו ,2015/16 • קלח 10 • רטסמסב ' עשת " ו ,2015/16 • R ב םדקתמ תונכת - R

  2. RSQLite RSQLite הלבטב שי תודש הזיא הליגר םינותנ תרגסמ איה ? Salaries • salaries.df • > head(salaries.df, 7) > dbListFields(con, "Salaries") yearID teamID lgID playerID salary [1] "yearID" "teamID" "lgID" "playerID" 1 1980 TOR AL stiebda01 55000 [5] "salary" 2 1981 NYA AL jacksre01 588000 • הלבטהמ םינותנ תרגסמ רוצינ : 3 1981 TOR AL stiebda01 85000 > salaries.df <- dbReadTable(con, "Salaries") 4 1982 TOR AL stiebda01 250000 5 1983 TOR AL stiebda01 450000 6 1984 TOR AL stiebda01 650000 7 1985 ATL NL barkele01 870000 > nrow(salaries.df) [1] 21464 5 ב םדקתמ תונכת - 6 קלח 10 • רטסמסב ' עשת " ו ,2015/16 • קלח 10 • רטסמסב ' עשת " ו ,2015/16 • R ב םדקתמ תונכת - R RSQLite RSQLite • התליאש : תחתמ תורוכשמה יטרפ 65,000$ ב -1985 • ב התליאשה התוא - : R > dbGetQuery(con, > attach(salaries.df) "SELECT * > salaries.df[yearID == 1985 & salary < 65000, ] FROM Salaries yearID teamID lgID playerID salary WHERE yearID=1985 AND salary < 65000") 47 1985 BAL AL sheetla01 60000 yearID teamID lgID playerID salary 81 1985 CAL AL clibust02 60000 1 1985 BAL AL sheetla01 60000 92 1985 CAL AL mccaski01 60000 2 1985 CAL AL clibust02 60000 112 1985 CHA AL guilloz01 60000 3 1985 CAL AL mccaski01 60000 148 1985 CIN NL brownto05 60000 4 1985 CHA AL guilloz01 60000 ... • תורושה ירפסמ םע הרק המ ? 5 1985 CIN NL brownto05 60000 ... 7 ב םדקתמ תונכת - 8 קלח 10 • רטסמסב ' עשת " ו ,2015/16 • קלח 10 • רטסמסב ' עשת " ו ,2015/16 • R ב םדקתמ תונכת - R

  3. םיליגרת RSQLite • הלבטב תורוכשמה לש םינשה חווט והמ ? • תיזכרמה הלבטה : תנשב 1990? • תצובקב תעצוממה תרוכשמה יהמ > dbListFields(con, "Master") ATL [1] "lahmanID" "playerID" "managerID" • ה תגיל תוצובקמ תחא לכב תעצוממה תרוכשמה יהמ - NL [4] "hofID" "birthYear" "birthMonth" תנשב 1990? [7] "birthDay" "birthCountry" "birthState" [10] "birthCity" "deathYear" "deathMonth" תנשב 1980 ? תנשבו • הצובקה לש אלמה םשה המ BOS [13] "deathDay" "deathCountry" "deathState" 1905? [16] "deathCity" "nameFirst" "nameLast" [19] "nameNote" "nameGiven" "nameNick" [22] "weight" "height" "bats" [25] "throws" "debut" "finalGame" [28] "college" "lahman40ID" "lahman45ID" [31] "retroID" "holtzID" "bbrefID" 9 ב םדקתמ תונכת - 10 קלח 10 • רטסמסב ' עשת " ו ,2015/16 • קלח 10 • רטסמסב ' עשת " ו ,2015/16 • R ב םדקתמ תונכת - R םיליגרת RSQLite • הדילה תנש תאו אלמה םשה תא תאצומה התליאש ובתכ > master.df <- dbReadTable(con, "Master") מ לחה ודלונש םינקחשה לכ לש -1988 > nrow(master.df) [1] 17676 • תאצומה התליאש ובתכןקחש לש אלמה םשה תא , תנש ולש הדובעה , הנש התואב תרוכשמהו , םינקחשה לכ רובע > head(master.df) לקשמב 290 הלעמו דנואפ • ההובגה תיתנשה תרוכשמה תא לביקש ןקחשה והימ תדדוב הנשב רתויב , איה המוהתיה ? 11 ב םדקתמ תונכת - 12 קלח 10 • רטסמסב ' עשת " ו ,2015/16 • קלח 10 • רטסמסב ' עשת " ו ,2015/16 • R ב םדקתמ תונכת - R

  4. שדח םינותנ סיסב תריצי השדח הלבט תריצי • ץוחנ אל רבכש םדוק םינותנ סיסבל םירבוחמ םא , יאדכ > dbSendQuery(con2, קתנתהל םדוק "CREATE TABLE Students (StID INTEGER, > dbDisconnect(con) StName TEXT, [1] TRUE • שדח םינותנ סיסבל רוביח רוצינ , הטיסרבינוא רובע : DptID INTEGER, StDeg TEXT)") > con2 <- dbConnect(SQLite(), <SQLiteResult> dbname="university.db") > dbListTables(con2) ץבוק רצונ ) קיר םייתניב ( םשב university.db • [1] "Students" > dbListTables(con2) • םימלש םניאש םירפסמל , סופיט ךירצ REAL character(0) 13 ב םדקתמ תונכת - 14 קלח 10 • רטסמסב ' עשת " ו ,2015/16 • קלח 10 • רטסמסב ' עשת " ו ,2015/16 • R ב םדקתמ תונכת - R הלבט תריציהשדח הלבטל םינותנ תסנכה • הקיר ןיידע הלבטה : • השקה ךרדב : תינדי םינותנ תסנכה > dbListFields(con2, "Students") > dbSendQuery(con2, [1] "StID" "StName" "DptID" "StDeg" "INSERT INTO Students > dbGetQuery(con2, "SELECT * FROM Students") VALUES(4658872, 'Gil Even', 5, 'BA')") [1] StID StName DptID StDeg <SQLiteResult> <0 rows> (or 0-length row.names) > dbReadTable(con2, "Students") > dbReadTable(con2, "Students") StID StName DptID StDeg [1] StID StName DptID StDeg 1 4658872 Gil Even 5 BA <0 rows> (or 0-length row.names) 15 ב םדקתמ תונכת - 16 קלח 10 • רטסמסב ' עשת " ו ,2015/16 • קלח 10 • רטסמסב ' עשת " ו ,2015/16 • R ב םדקתמ תונכת - R

  5. הלבטל םינותנ תסנכה הלבטל םינותנ תסנכה • םייקלח םינותנ הלבטל סינכהל רשפא > dbSendQuery(con2, "INSERT INTO Students > dbSendQuery(con2, VALUES(5982760, 'Jihan Ganem', 2, 'MA')") "INSERT INTO Students <SQLiteResult> (StID, StName, StDeg) > dbReadTable(con2, "Students") VALUES(2233445, 'Zvika Pick', 'BA')") <SQLiteResult> StID StName DptID StDeg > dbReadTable(con2, "Students") 1 4658872 Gil Even 5 BA StID StName DptID StDeg 2 5982760 Jihan Ganem 2 MA 1 4658872 Gil Even 5 BA 2 5982760 Jihan Ganem 2 MA 3 2233445 Zvika Pick NA BA 17 ב םדקתמ תונכת - 18 קלח 10 • רטסמסב ' עשת " ו ,2015/16 • קלח 10 • רטסמסב ' עשת " ו ,2015/16 • R ב םדקתמ תונכת - R הלבטב םינותנ ןוכדע הלבטל םינותנ תסנכה • הקלחמב ינש ראות ידומילל רבוע ליג טנדוטסה םא • ע םג רשפא" היצקנופה י read.csv.sql תרחא : )סרוקה רתאב (בותכ: ץבוקב Departments.csv • > dbSendQuery(con2, "UPDATE Students SET DptID = 1, StDeg = 'MA' WHERE StID = 4658872") <SQLiteResult> > dbReadTable(con2, "Students") StID StName DptID StDeg 1 4658872 Gil Even 1 MA 2 5982760 Jihan Ganem 2 MA • טסקט ץבוקכ םג ותוא ךורעל רשפא 3 2233445 Zvika Pick NA BA 19 ב םדקתמ תונכת - 20 קלח 10 • רטסמסב ' עשת " ו ,2015/16 • קלח10 • רטסמסב 'עשת"ו ,2015/16 • R ב םדקתמ תונכת - R

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend