思行勤业分享 http://blog.sciencenet.cn/u/xiangxing 记录心路历程,探索科研领地,交流学习经验,分享知识,结识友朋。

博文

Introduction to R Data Type

已有 4320 次阅读 2014-4-26 18:20 |个人分类:数据生活|系统分类:科研笔记| Type, Data

text-indent:2em; text-align:left;

#

# Copyright (c) 2010-~ siqin.hou All rights reserved.

#

# This source code is released for free distribution under the terms of the

# GNU General Public License

#

# ---Author: siqin<siqin.hou@gmail.com>

# Date-Time: 2014-04-26 16:38:07

# File-Name: Introduction_to_R.r

# --Version: 1.0

# -Function: Statistics One课程中提供的Introduction to R, Andrew Conway

# -Source From:

# -菜鸟学R语言:用R做数据分析(零基础)之前言 | Yang Liu fr

# -http://yangliufr.com/2013/09/24/r-introduction/

# -菜鸟学R语言:用R做数据分析(零基础)之数据类型 | Yang Liu fr

# -http://yangliufr.com/2013/09/29/r-introduction-i/ 

# -Andrew Conway @ coursera.org: Statistics One

# -https://accounts.coursera.org/signin?course_id=970574&r=https%3A%2F%2Fclass.coursera.org%2Fstats1-002%2Flecture&user_action=class&topic_name=Statistics%20One

#首先,下载与安装R软件,网站:http://www.r-project.org/。

#当然,如果你一定是找不到这个网站中R语言的程序位置,那么请看这里:http://ftp.ctex.org/mirrors/CRAN/,在这里,请点击install R for the first time,

#安装时请注意一点:安装路径不要有中文字符,以避免一些不必要的麻烦。

#推荐大家再安装另外一个软件,叫做Rstudio。http://www.rstudio.com/ide/download/

# --------------------------------------------------------------------------------

# Basic mathematical operations# 四则运算

# --------------------------------------------------------------------------------

3 + 4

5 * 5

12 / 3

5^5

# 这几行代码就充分展现出了R的优势之一,即时互动。大家只要在console中输入以上一行代码,就可以直接得到结果,省去了传统语言编译的过程(并不是说R中不存在编译)。

#> 3 + 4

#[1] 7

#> 5 * 5

#[1] 25

#> 12 / 3

#[1] 4

#> 5^5

#[1] 3125

#以上就是运行结果。

# --------------------------------------------------------------------------------

# R objects  R的对象

# --------------------------------------------------------------------------------

#R的数据对象有向量,列表,矩阵,数据框,依据不同需求使用不同数据对象类型。

#vector class/vector type查看数据对象的类型

class(v)

# ------------------------------------------------------------

# Vector 向量

## Most basic object in R

## Contains elements of the same class

## Can be: character, numeric, integer, complex, logical(True/False))

#向量(vector)是R中最基本的对象。

#R的对象中大部分能且只能包含同一类型的若干个元素,但是list除外,可以包含若干类的若干个元素,这也是为什么要有list的原因。

#常见的向量有:字符向量,数值向量,整数向量,复数向量,

# Create a vector,使用Combine首字母,同类型的对象集合

#下面来创建一个含有数值1,3,5,7的向量

v=c(1,3,5,7) #c() Combine是集合的意思

v

class(v)

# Vector Type 向量中的数据类型

x <- c(0.5, 0.6) ## numeric 数值向量

x <- c(TRUE, FALSE) ## logical 逻辑向量

x <- c(T, F) ## logical 逻辑向量

x <- c("a", "b", "c") ## character 字符向量

x <- 9:29 ## integer 整数向量

x <- c(1+0i, 2+4i) ## complex 复数向量

class(x)

#[1] "complex"

#但,如果把两种向量c()到一起,会得到什么样的向量集合呢?

y <- c(1.7, "a") ## numeric+character->character

y <- c(TRUE, 2) ## logical+numeric—>numeric

y <- c("a", TRUE) ## character+logical->character

#你会发现,这时向量集会被强制(coercion)转成同一类,为了符合向量的定义。

#这时,如果你需要提取其中的假向量用于运算,那就需要as.**functions,

#其中**可以是numeric,logical,character,complex等。如下,

#methods(as)或者methods(is)查看

x <- 0:6

class(x) #class()用于查看向量的类

#[1] "integer"

as.numeric(x)

[1] 0 1 2 3 4 5 6

as.logical(x)

#[1] FALSE TRUE TRUE TRUE TRUE TRUE TRUE

as.character(x)

#[1] "0" "1" "2" "3" "4" "5" "6"

as.complex(x)

#[1] 0+0i 1+0i 2+0i 3+0i 4+0i 5+0i 6+0i

#如果使用as.**function把某类向量转化为另一类,则会得到NAs。如,

x <- c("a", "b", "c")

as.numeric(x)

#[1] NA NA NA

#Warning message:

#NAs introduced by coercion

as.logical(x)

#[1] NA NA NA

#有时你可能还需要空白向量,可以用vector()来创建,例如创建一个长度为10的数值向量x,

x <- vector("numeric", length = 10)

x

#[1] 0 0 0 0 0 0 0 0 0 0

# ------------------------------------------------------------

# List 列表

## (Vector with different class of objects)

## 包含不同类型的对象

l=list("Blue", 2, 5, "Red")

l

#--------------------------------------------------------------------------------

# Create a matrix

# matrix (矩阵)

#矩阵(pl. matrices)具有维度(dimension)属性(attribute)的向量。该维度属性本身是一个长度为2的整数向量(nrow,ncol),行数和列数。

#--------------------------------------------------------------------------------

m <- matrix(nrow = 2, ncol = 3)

m

#[,1] [,2] [,3]

#[1,] NA NA NA

#[2,] NA NA NA

#function dim():Retrieve or set the dimension of an object

#Useage

#(x),Retrieve the dimension of an object获取矩阵的维度属性

#dim(x) <- value,set the dimension of an object设置矩阵的维度属性

#Examples

#x <- 1:12 ; dim(x) <- c(3,4)

#x

# simple versions of nrow and ncol could be defined as follows

#nrow0 <- function(x) dim(x)[1]

#nrow0(m)

#ncol0 <- function(x) dim(x)[2]

#ncol0(m)

dim(m)

#Retrieve the dimension of matrix x

#获取矩阵的维度属性

#[1] 2 3

#rows: 2, colluns: 3, 2x3的矩阵

# Check the attributes

attributes(m)

#获取矩阵的维度属性

#$dim

#[1] 2 3

# Call a particular cell in a matrix

# 调取矩阵中特定位置的元素

m

m[1,2]

## Matrix creation is column-wise

#在R中建矩阵是遵从列优先(column-wise)的原则,即,优先按照顺序从左上角开始向右一列一列地填充。如,

m <- matrix(1:6, nrow = 2, ncol = 3)

#m=matrix(1:6,2,3)

m

#[,1] [,2] [,3]

#[1,] 1 3 5

#[2,] 2 4 6

#在了解了这个特性之后,我们就可使用dim()来为向量集创建矩阵了,大家只要注意列优先原则即可,如,

# Create a matrix from a vector

#从向量开始创建啊一个矩阵

#第一步,创建一个同类型对象集合--向量

m=matrix(1:10)

#m <- 1:10

m

#[1] 1 2 3 4 5 6 7 8 9 10

# Then add dimensionality

#第二步,设置矩阵m的维度属性,决定了对象的排列情况

dim(m)=c(2,5)

#dim(m) <- c(2, 5)

m

#[,1] [,2] [,3] [,4] [,5]

#[1,] 1 3 5 7 9

#[2,] 2 4 6 8 10

# Create a matrix by binding columns or rows

#此外,在R中,你也可以使用cbind()(column-binding)和rbind()(row-binding)functions来创建矩阵。如,

x <- 1:3

y <- 10:12

# Create a matrix by binding column

cbind(x, y)

#       x y

#[1,]   1 10

#[2,]   2 11

#[3,]   3 12

# Create a matrix by binding rows

rbind(x, y)

#[,1] [,2] [,3]

#x 1 2 3

#y 10 11 12

#--------------------------------------------------------------------------------

# Dataframes 数据框

## Different than matrices => can store different classes of objects

# 能存储不同类型的对象

## Usually called with read.table()

#--------------------------------------------------------------------------------

# Create a dataframe创建数据框对象

d=data.frame(subjectID=1:5,gender=c("M","F","F","M","F"),score=c(8,3,6,5,5))

d

# Number of rows 获取数据框的行数

nrow(d)

# Number of columns 获取数据框的列数

ncol(d)

# Check the attributes 获取数据框的维度属性

attributes(d)

# Call a particular cell in a dataframe

# 调取数据框中某一个特定的元素

d[2,1]

d[1,2]

# Display dataframe 显示数据框,注意大小写,是首字母大写的V

View(d)

# Edit dataframe 编辑数据框的数据

edit(d)

#--------------------------------------------------------------------------------

# 附录

#--------------------------------------------------------------------------------

# Getting help on a function

# 获得某一个函数的使用帮助

?functionname

#get all packages

#获得当前安装的所有R包,看看需不需要安装相应的包

.packages(all.available = TRUE)

# Download and install packages

# 下载与安装R包

## Need to specify CRAN the 1st time

# 第一次使用需要设置 CRAN,选择使用哪个国家的服务器下载,就近原则

install.packages("psych")

# Load package

# 加载R包

# library("package_name")

library("psych")

#RODBC read xls file data example

#使用RODBC包读取Excel文件.xls里的数据

library("RODBC")

z <- odbcConnectExcel("rexceltest.xls")

data <- sqlFetch(z,"Sheet1")

close(z)

#R中if else 的写法-else不能单独一行

if(){

}else{

}

格式化不了,科学网的编辑器怎么了?



https://blog.sciencenet.cn/blog-250383-788859.html

上一篇:从接触Photoshop到立志从事数据分析与展现之路
下一篇:CentOS 6.5 yum install R
收藏 IP: 106.120.15.*| 热度|

0

该博文允许注册用户评论 请点击登录 评论 (0 个评论)

数据加载中...

Archiver|手机版|科学网 ( 京ICP备07017567号-12 )

GMT+8, 2024-4-18 12:47

Powered by ScienceNet.cn

Copyright © 2007- 中国科学报社

返回顶部