SlideShare ist ein Scribd-Unternehmen logo
1 von 17
R Language

   note
Data analysis and graphics with R
• R is a language and environment for statistical
  computing and graphics, similar to the S
  language originally developed at Bell Labs
Common commands
•   getwd() 读工作目录.
•   setwd(“c:/”) 改变工作目录
•   ls() 显示当前workspace的变量
•   rm(objectlist) Remove (delete) one or more objects.
•   options() View or set current options.
•   savehistory("myfile") Save the commands history to myfile ( default =
•   .Rhistory).
•   loadhistory("myfile") Reload a command's history (default = .Rhistory).
•   save.image("myfile") Save the workspace to myfile (default = .RData).
•   save(objectlist,file="myfile") Save specific objects to a file.
•   load("myfile") Load a workspace into the current session (default =
•   .RData).
R Data Structures
•
Vector
• Vector
  –   c (2,3,1,6,5,2)
  – X = c (1:9)
  – X*2 向量乘
      • 2,4,6,8,10,12,14,16,18
Array
• array(c(1:9), dim=c(3,3))
  –    [,1] [,2] [,3]
  –   [1,] 1 4 7
  –   [2,] 2 5 8
  –   [3,] 3 6 9
Matrix
myymatrix <- matrix(
     vector,
     nrow=number_of_rows,
     ncol=number_of_columns,
     byrow=logical_value,
     dimnames=list(
         char_vector_rownames,
         char_vector_colnames
     )
)
M = matrix(c(1:10), nrow=2, ncol=5, byrow=T)
                                 byrow=T横向填充                byrow=F竖向填充
                                 [,1] [,2] [,3] [,4] [,5]   [,1] [,2] [,3] [,4] [,5]
                                 [1,] 1 2 3 4 5             [1,] 1 3 5 7 9
                                 [2,] 6 7 8 9 10            [2,] 2 4 6 8 10
Matrix
a=matrix(c(1:10), nrow=2, ncol=5, byrow=T,
dimnames=list(c('R1','R2'),c('a','b','c','d','e')))
                         abcd e
                        R1 1 2 3 4 5
                        R2 6 7 8 9 10

• 选择数据:
    – a[1,2] 结果:2
    – a[2,c(3:5)] 结果:8,9,10
Array
• n1=c('r1','r2')
• n2=c('col1','col2','col3')
• n3=c('p1','p2','p3','p4')
•array(c(1:24),c(2,3,4),dimnames=list(n1,n2,n3)
 )
三维 数组
data frame
•   name=c('jack','tom','joe','linda')
•   age=c('21','23','20','19')
•   city=c('bj','sh','sh','bj')
•   d = data.frame(name,age,city)
•   Query:
    –   d['age']
    –   d[c('age','city')]
    –   d[1:2]
    –   d$age
• Edit:
    – d = edit(d)
data frame
• table(d$city,d$name)
  – jack joe linda tom
  – bj 1 0 1 0
  – sh 0 1 0 1


    attach(d)            with(d,{
    summary(name)        summary(name)
    …                    …
    detach(d)            })
Lists
• Lists are the most complex of the R data types

•   g <- "My First List"
•   h <- c(25, 26, 18, 39)
•   j <- matrix(1:10, nrow=5)
•   k <- c("one", "two", "three")
•   mylist <- list(title=g, ages=h, j, k)
data input
•
data input
• mydata <-
  data.frame(age=numeric(0),       gender=char
  acter(0), weight=numeric(0))
  mydata <- edit(mydata)

• mydataframe =
  read.table("c:/a.txt", header=FALSE, sep="t",
  row.names="name")
  – header:是否把第一行作为column name
  – row.names:标识
data input
• read excel:
  –   library(RODBC)
  –   channel <- odbcConnectExcel("c:/t.xlsx")
  –   mydataframe <- sqlFetch(channel, "Sheet1")
  –   odbcClose(channel)

• excel2007:
  – library(xlsx)
  – workbook <- "c:/myworkbook.xlsx"
  – mydataframe <- read.xlsx(workbook, 1)
data input
• library(RODBC)
• myconn <-odbcConnect("mydsn", uid="Rob",
  pwd="aardvark")
• crimedat <- sqlFetch(myconn, Crime)
• pundat <- sqlQuery(myconn, "select * from
  Punishment")
• close(myconn)
data export
•   png(), jpeg(),
•   bmp(), tiff(), xfig()
•   pdf("mygraph.pdf")
•   …….
•   dev.off()

Weitere ähnliche Inhalte

Was ist angesagt?

peRm R group. Review of packages for r for market data downloading and analysis
peRm R group. Review of packages for r for market data downloading and analysispeRm R group. Review of packages for r for market data downloading and analysis
peRm R group. Review of packages for r for market data downloading and analysis
Vyacheslav Arbuzov
 
Seminar PSU 09.04.2013 - 10.04.2013 MiFIT, Arbuzov Vyacheslav
Seminar PSU 09.04.2013 - 10.04.2013 MiFIT, Arbuzov VyacheslavSeminar PSU 09.04.2013 - 10.04.2013 MiFIT, Arbuzov Vyacheslav
Seminar PSU 09.04.2013 - 10.04.2013 MiFIT, Arbuzov Vyacheslav
Vyacheslav Arbuzov
 

Was ist angesagt? (20)

CLUSTERGRAM
CLUSTERGRAMCLUSTERGRAM
CLUSTERGRAM
 
peRm R group. Review of packages for r for market data downloading and analysis
peRm R group. Review of packages for r for market data downloading and analysispeRm R group. Review of packages for r for market data downloading and analysis
peRm R group. Review of packages for r for market data downloading and analysis
 
Seminar PSU 09.04.2013 - 10.04.2013 MiFIT, Arbuzov Vyacheslav
Seminar PSU 09.04.2013 - 10.04.2013 MiFIT, Arbuzov VyacheslavSeminar PSU 09.04.2013 - 10.04.2013 MiFIT, Arbuzov Vyacheslav
Seminar PSU 09.04.2013 - 10.04.2013 MiFIT, Arbuzov Vyacheslav
 
imager package in R and examples..
imager package in R and examples..imager package in R and examples..
imager package in R and examples..
 
Advanced Data Visualization Examples with R-Part II
Advanced Data Visualization Examples with R-Part IIAdvanced Data Visualization Examples with R-Part II
Advanced Data Visualization Examples with R-Part II
 
Data visualization using the grammar of graphics
Data visualization using the grammar of graphicsData visualization using the grammar of graphics
Data visualization using the grammar of graphics
 
R meets Hadoop
R meets HadoopR meets Hadoop
R meets Hadoop
 
Introduction to R
Introduction to RIntroduction to R
Introduction to R
 
Geo Spatial Plot using R
Geo Spatial Plot using R Geo Spatial Plot using R
Geo Spatial Plot using R
 
Mosaic plot in R.
Mosaic plot in R.Mosaic plot in R.
Mosaic plot in R.
 
Data visualization with multiple groups using ggplot2
Data visualization with multiple groups using ggplot2Data visualization with multiple groups using ggplot2
Data visualization with multiple groups using ggplot2
 
Py lecture5 python plots
Py lecture5 python plotsPy lecture5 python plots
Py lecture5 python plots
 
NumPy Refresher
NumPy RefresherNumPy Refresher
NumPy Refresher
 
Numpy python cheat_sheet
Numpy python cheat_sheetNumpy python cheat_sheet
Numpy python cheat_sheet
 
C++ ammar .s.q
C++  ammar .s.qC++  ammar .s.q
C++ ammar .s.q
 
Experement no 6
Experement no 6Experement no 6
Experement no 6
 
Python programing
Python programingPython programing
Python programing
 
Basic Calculus in R.
Basic Calculus in R. Basic Calculus in R.
Basic Calculus in R.
 
Advanced Data Visualization in R- Somes Examples.
Advanced Data Visualization in R- Somes Examples.Advanced Data Visualization in R- Somes Examples.
Advanced Data Visualization in R- Somes Examples.
 
Mat lab lecture part 1
Mat lab lecture part 1Mat lab lecture part 1
Mat lab lecture part 1
 

Andere mochten auch (7)

Taito
TaitoTaito
Taito
 
Hadoop 20111215
Hadoop 20111215Hadoop 20111215
Hadoop 20111215
 
Hadoop 20111117
Hadoop 20111117Hadoop 20111117
Hadoop 20111117
 
Statistics
StatisticsStatistics
Statistics
 
Hive data migration (export/import)
Hive data migration (export/import)Hive data migration (export/import)
Hive data migration (export/import)
 
Which Hadoop Distribution to use: Apache, Cloudera, MapR or HortonWorks?
Which Hadoop Distribution to use: Apache, Cloudera, MapR or HortonWorks?Which Hadoop Distribution to use: Apache, Cloudera, MapR or HortonWorks?
Which Hadoop Distribution to use: Apache, Cloudera, MapR or HortonWorks?
 
Ios
IosIos
Ios
 

Ähnlich wie R

R Programming.pptx
R Programming.pptxR Programming.pptx
R Programming.pptx
kalai75
 
Learning notes of r for python programmer (Temp1)
Learning notes of r for python programmer (Temp1)Learning notes of r for python programmer (Temp1)
Learning notes of r for python programmer (Temp1)
Chia-Chi Chang
 

Ähnlich wie R (20)

Data analysis with R
Data analysis with RData analysis with R
Data analysis with R
 
R programming language
R programming languageR programming language
R programming language
 
Big datacourse
Big datacourseBig datacourse
Big datacourse
 
R and data mining
R and data miningR and data mining
R and data mining
 
R Programming.pptx
R Programming.pptxR Programming.pptx
R Programming.pptx
 
Language R
Language RLanguage R
Language R
 
R programming
R programmingR programming
R programming
 
R for you
R for youR for you
R for you
 
Day 1d R structures & objects: matrices and data frames.pptx
Day 1d   R structures & objects: matrices and data frames.pptxDay 1d   R structures & objects: matrices and data frames.pptx
Day 1d R structures & objects: matrices and data frames.pptx
 
R language, an introduction
R language, an introductionR language, an introduction
R language, an introduction
 
R training3
R training3R training3
R training3
 
An overview of Python 2.7
An overview of Python 2.7An overview of Python 2.7
An overview of Python 2.7
 
A tour of Python
A tour of PythonA tour of Python
A tour of Python
 
R Functions in Dataframe.pptx
R Functions in Dataframe.pptxR Functions in Dataframe.pptx
R Functions in Dataframe.pptx
 
A quick introduction to R
A quick introduction to RA quick introduction to R
A quick introduction to R
 
Introduction to R
Introduction to RIntroduction to R
Introduction to R
 
Week7
Week7Week7
Week7
 
Ggplot2 v3
Ggplot2 v3Ggplot2 v3
Ggplot2 v3
 
Learning notes of r for python programmer (Temp1)
Learning notes of r for python programmer (Temp1)Learning notes of r for python programmer (Temp1)
Learning notes of r for python programmer (Temp1)
 
Data import-cheatsheet
Data import-cheatsheetData import-cheatsheet
Data import-cheatsheet
 

Kürzlich hochgeladen

Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 

Kürzlich hochgeladen (20)

Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot ModelNavi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
Ransomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfRansomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdf
 

R

  • 1. R Language note
  • 2. Data analysis and graphics with R • R is a language and environment for statistical computing and graphics, similar to the S language originally developed at Bell Labs
  • 3. Common commands • getwd() 读工作目录. • setwd(“c:/”) 改变工作目录 • ls() 显示当前workspace的变量 • rm(objectlist) Remove (delete) one or more objects. • options() View or set current options. • savehistory("myfile") Save the commands history to myfile ( default = • .Rhistory). • loadhistory("myfile") Reload a command's history (default = .Rhistory). • save.image("myfile") Save the workspace to myfile (default = .RData). • save(objectlist,file="myfile") Save specific objects to a file. • load("myfile") Load a workspace into the current session (default = • .RData).
  • 5. Vector • Vector – c (2,3,1,6,5,2) – X = c (1:9) – X*2 向量乘 • 2,4,6,8,10,12,14,16,18
  • 6. Array • array(c(1:9), dim=c(3,3)) – [,1] [,2] [,3] – [1,] 1 4 7 – [2,] 2 5 8 – [3,] 3 6 9
  • 7. Matrix myymatrix <- matrix( vector, nrow=number_of_rows, ncol=number_of_columns, byrow=logical_value, dimnames=list( char_vector_rownames, char_vector_colnames ) ) M = matrix(c(1:10), nrow=2, ncol=5, byrow=T) byrow=T横向填充 byrow=F竖向填充 [,1] [,2] [,3] [,4] [,5] [,1] [,2] [,3] [,4] [,5] [1,] 1 2 3 4 5 [1,] 1 3 5 7 9 [2,] 6 7 8 9 10 [2,] 2 4 6 8 10
  • 8. Matrix a=matrix(c(1:10), nrow=2, ncol=5, byrow=T, dimnames=list(c('R1','R2'),c('a','b','c','d','e'))) abcd e R1 1 2 3 4 5 R2 6 7 8 9 10 • 选择数据: – a[1,2] 结果:2 – a[2,c(3:5)] 结果:8,9,10
  • 9. Array • n1=c('r1','r2') • n2=c('col1','col2','col3') • n3=c('p1','p2','p3','p4') •array(c(1:24),c(2,3,4),dimnames=list(n1,n2,n3) ) 三维 数组
  • 10. data frame • name=c('jack','tom','joe','linda') • age=c('21','23','20','19') • city=c('bj','sh','sh','bj') • d = data.frame(name,age,city) • Query: – d['age'] – d[c('age','city')] – d[1:2] – d$age • Edit: – d = edit(d)
  • 11. data frame • table(d$city,d$name) – jack joe linda tom – bj 1 0 1 0 – sh 0 1 0 1 attach(d) with(d,{ summary(name) summary(name) … … detach(d) })
  • 12. Lists • Lists are the most complex of the R data types • g <- "My First List" • h <- c(25, 26, 18, 39) • j <- matrix(1:10, nrow=5) • k <- c("one", "two", "three") • mylist <- list(title=g, ages=h, j, k)
  • 14. data input • mydata <- data.frame(age=numeric(0), gender=char acter(0), weight=numeric(0)) mydata <- edit(mydata) • mydataframe = read.table("c:/a.txt", header=FALSE, sep="t", row.names="name") – header:是否把第一行作为column name – row.names:标识
  • 15. data input • read excel: – library(RODBC) – channel <- odbcConnectExcel("c:/t.xlsx") – mydataframe <- sqlFetch(channel, "Sheet1") – odbcClose(channel) • excel2007: – library(xlsx) – workbook <- "c:/myworkbook.xlsx" – mydataframe <- read.xlsx(workbook, 1)
  • 16. data input • library(RODBC) • myconn <-odbcConnect("mydsn", uid="Rob", pwd="aardvark") • crimedat <- sqlFetch(myconn, Crime) • pundat <- sqlQuery(myconn, "select * from Punishment") • close(myconn)
  • 17. data export • png(), jpeg(), • bmp(), tiff(), xfig() • pdf("mygraph.pdf") • ……. • dev.off()