• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

R语言学习笔记

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

 

1.R是一种可编程的语言。

2.所有R的函数和数据集是保存在程序包里面的。只有当一个包被载入时,它的内容才可以被访问。在另外版安装文件中,已经包含的程序包有:base一R的基础模块、mle一极大似然估计模块、ts一时间序列分析模块、mva一多元统计分析模块、survival一生存分析模块等等.

 

R的使用

Data Types

R has a wide variety of data types including scalars, vectors (numerical, character, logical), matrices, data frames, and lists.

  • Decimal values like 4.5 are called numerics.
  • Natural numbers like 4 are called integers. Integers are also numerics.
  • Boolean values (TRUE or FALSE) are called logical.
  • Text (or string) values are called characters.

Creating New Variables

Use the assignment operator <- to create new variables.

# An example of computing the mean with variables
mydata$sum <- mydata$x1 + mydata$x2
mydata$mean <- (mydata$x1 + mydata$x2)/2

Functions

Almost everything in R is done through functions. A function is a piece of code written to carry out a specified task; it may accept arguments or parameters (or not) and it may return one or more values (or not!). In R, a function is defined with the construct:

function ( arglist ) {body}

Importing Data

Importing data into R is fairly simple. R offers options to import many file types, from CSVs to databases.

For example, this is how to import a CSV into R.

# first row contains variable names, comma is separator 
# assign the variable id to row names
# note the / instead of \ on mswindows systems 

mydata <- read.table("c:/mydata.csv", header=TRUE, 
   sep=",", row.names="id")

Descriptive Statistics

R provides a wide range of functions for obtaining summary statistics. One way to get descriptive statistics is to use the sapply( ) function with a specified summary statistic.

Below is how to get the mean with the sapply( ) function:

# get means for variables in data frame mydata
# excluding missing values 
sapply(mydata, mean, na.rm=TRUE)

Possible functions used in sapply include mean, sd, var, min, max, median, range, and quantile.

Plotting in R

In R, graphs are typically created interactively. Here is an example:

# Creating a Graph
attach(mtcars)
plot(wt, mpg) 
abline(lm(mpg~wt))
title("Regression of MPG on Weight")

The plot( ) function opens a graph window and plots weight vs. miles per gallon. The next line of code adds a regression line to this graph. The final line adds a title.

Packages

Packages are collections of R functions, data, and compiled code in a well-defined format. The directory where packages are stored is called the library. R comes with a standard set of packages. Others are available for download and installation. Once installed, they have to be loaded into the session to be used.

.libPaths() # get library location 
library()   # see all packages installed 
search()    # see packages currently loaded

Getting Help

Once R is installed, there is a comprehensive built-in help system. At the program's command prompt you can use any of the following:

help.start()   # general help
help(foo)      # help about function foo
?foo           # same thing 
apropos("foo") # list all functions containing string foo
example(foo)   # show an example of function foo

 

R里的+不能用来连接字符串

Useful data frame functions

  • head() - shown first 6 rows
  • tail() - show last 6 rows
  • dim() - returns the dimensions
  • nrow() - number of rows
  • ncol() - number of columns
  • str() - structure of each column
  • names() - shows the names attribute for a data frame, which gives the column names.

 

一个例子

mydata <- read.csv("/Users/xiao/Downloads/samplecsv/1.01.\ Simple\ linear\ regression.csv", header=TRUE)
SAT <- mydata[,1]
GPA <- mydata[,2]
plot(GPA,SAT)
abline(lm(SAT~GPA),col="blue")
#coefficient
cor(SAT,GPA)


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
R语言中向量&amp;矩阵&amp;数组&amp;数据框&amp;列表的区别与联系 ...发布时间:2022-07-18
下一篇:
第一章,R语言介绍发布时间:2022-07-18
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap