总结一下“入门3R”(Reading, ‘Riting, ‘Rrithmetic)中的读和写,不同的数据结构下的读写还是有点区别的。

vector

命名

1
2
month.days<-c(31,28,31,30,31,30,31,31,30,31,30,31)
names(month.days)<-month.name

操作文本

1.文本分离

1
2
pangram<-"The quick brown fox jumps over the lazy dog"
strsplit(pangram," ")

strplit()函数将pangram用空格切开,这个函数的返回值是list

1
words<-strsplit(pangram," ")[[1]]

可以取出字符串数组

2.文本连接

1
2
paste(LETTERS[1:5],1:5,sep="_",collapse="---")
paste("Sample",1:5)

用空格连接words中的元素,paste()接收的参数应该是多个变量,sep决定多个向量之间的连接符,而collapse决定统一向量中的元素怎么合并。

3.文本排序

1
sort(letters,decreasing=TRUE)

4.查找文本

1
2
substr(state.name,start=3,stop=6) 
grep("New",state.name)####通过模式查找

grep(pattern,x)返回的是符合pattern的元素的在x中的位置

5.文本替换

1
gsub("cheap","sheep's","A wolf in cheap clothing")
1
2
x<-c("file_a.csv","file_b.csv","file_c.csv")
y<-gsub("file_","",x)

因子分类

factor(x,levels,labels)可以创建R因子,而levels指的是x的输入值,labels表示新创建的因子的输出值。

因子转换

1
2
3
4
5
numbers<-factor(c(9,8,10,8,9))
str(numbers)
as.character(numbers)###返回字符型元素
as.numeric(numbers)###返回因子的内部表示
as.numeric(as.character(numbers))###返回数值型元素

有序因子

类别数据的统计
1
table(state.region)
有序变量
  • 使用factor()函数,并且指定参数ordered=TRUE
  • 使用ordered()函数

matrix

1
2
3
4
5
matrix(data,ncol,nrow,byrow)
dim()###查看矩阵维度
rbind()###将向量按行组成矩阵
cbind()###将向量按列组成矩阵
cbind(1:3,4:6,matrix(7:12,ncol=2))

索引、修改和命名

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
first.matrix<-matrix[1:12,ncol=4,byrow=TRUE]
#############取值
first.matrix[1:2,2:3]
first.matrix[2:3,]###数值索引
first.matrix[-2,-3]###提取除了第2行,第3列外全部数据
first.matrix[-c(1,3),]###维度降低成向量
first.matrix[2, ,drop=大专栏  array, matrix, list and dataframeeral">FALSE]###维度不降低,仍是矩阵
#############修改
first.matrix[3,2]<-4
first.matrix[2,]<-c(1,3)
first.matrix[1:2,3:4]<-c(8,4,2,1)
#############行列命名
rownames(x)<-c('a', 'b')
colnames(x)<-c('c', 'd')
colnames(x)[1]<-'aa'
x['b',]###用名称作为索引

计算

1
2
3
t()###转置
solve()###求逆
x %*% t(x)###相乘

array

向量和矩阵都是数组.

1
2
array(1:24,dim=c(3,4,2))###创建一个三维数组
dim(x)<-c(3,4,2)###改变向量x的维度

data.frame

由矩阵创建 x.df<-as.data.frame(x)

由向量创建 data<-data.frame(x,y,z)

如果创建的变量是字符串类型,R会自动转换成因子,可以用stringAsFactor=FALSE保持字符串类型

1
2
names(data)[2]<-'B' ###命名表头
rownames(data)<- c('a','b','c') ###命名观测

操作data.frame中的值

data.frame并不是向量,而是一组向量列表。但是数据操作时可以当做矩阵来处理,访问单个变量时可以用$,访问多个变量时可以用[]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#########修改值
y<-rbind(x,new.obs) ###添加单个观测
y<-rbind(x,'d'=new.obs) ###显式制定行名 new.obs<-data.frame(A=c(1,2),B=c(2,3))
rownames<(new.obs)<-c('e','f')
y<-rbind(x,new.obs) ###添加多个观测 x[c('e','f'),]<-matrix(c(1,1,2,4),ncol=2) ###使用索引添加多个值 ##########修改变量
x$C<-new.var ###添加一个变量
new.df<-data.frame(newvar1,newvar2)
x<-cbind(x,new.df) ###添加多个变量

list

1
2
3
4
5
6
7
8
#######创建list
new.list<-list(x,y)###无命名列表
new.nlist<-list(name1=x,name2=y)###命名列表
names(new.nlist)###获取列表名称
length(new.list)###获取列表长度 ########提取列表中的元素
###

提取列表中的元素

  • 使用[[]]返回元素本身
  • 使用[]返回选定元素的列表
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#########修改元素值
new.nlist[[1]]<-x
new.nlist[['name1']]<-x
new.nlist$name1<-x
new.nlist[1]<-list(x)
new.nlist[1:2]<-list(x,y) ##########移除元素
new.nlist[[1]]<-NULL
new.nlist[['name1']]<-NULL
new.nlist$name1<-NULL
new.nlist[1]<-list(NULL) ##########添加元素
new.nlist$name3<-z
new.nlist[['name3']]<-z
new.nlist['name3']<-list(z) ##########列表合成
z<-list(z)
c(new.nlist,z)

array, matrix, list and dataframe的更多相关文章

  1. numpy中list array matrix比较

    用python中的numpy包的时候不小心踩了array和matrix的大坑,又引申一下比较list array matrix之间的异同.数据结构(Data Structures)基本上人如其名——它 ...

  2. array / matrix subarray/submatrix sum

    Maximal Subarray Sum : O(n) scan-and-update dynamic programming, https://en.wikipedia.org/wiki/Maxim ...

  3. Pramp mock interview (4th practice): Matrix Spiral Print

    March 16, 2016 Problem statement:Given a 2D array (matrix) named M, print all items of M in a spiral ...

  4. C#+无unsafe的非托管大数组(large unmanaged array in c# without 'unsafe' keyword)

    C#+无unsafe的非托管大数组(large unmanaged array in c# without 'unsafe' keyword) +BIT祝威+悄悄在此留下版了个权的信息说: C#申请一 ...

  5. C++_Eigen函数库用法笔记——The Array class and Coefficient-wise operations

    The advantages of Array Addition and subtraction Array multiplication abs() & sqrt() Converting ...

  6. 【LeetCode】74. Search a 2D Matrix

    Difficulty:medium  More:[目录]LeetCode Java实现 Description Write an efficient algorithm that searches f ...

  7. Spark MLlib 之 大规模数据集的相似度计算原理探索

    无论是ICF基于物品的协同过滤.UCF基于用户的协同过滤.基于内容的推荐,最基本的环节都是计算相似度.如果样本特征维度很高或者<user, item, score>的维度很大,都会导致无法 ...

  8. pandas 数据结构基础与转换

    pandas 最常用的三种基本数据结构: 1.dataFrame: https://pandas.pydata.org/pandas-docs/stable/generated/pandas.Data ...

  9. pandas 之 特征工程

    import numpy as np import pandas as pd So far(到目前为止) in this chapter we've been concerned with rearr ...

随机推荐

  1. [Algo] 649. String Replace (basic)

    Given an original string input, and two strings S and T, replace all occurrences of S in input with ...

  2. [Algo] 280. Sort With 2 Stacks

    Given an array that is initially stored in one stack, sort it with one additional stacks (total 2 st ...

  3. 吴裕雄--天生自然TensorFlow高层封装:使用TFLearn处理MNIST数据集实现LeNet-5模型

    # 1. 通过TFLearn的API定义卷机神经网络. import tflearn import tflearn.datasets.mnist as mnist from tflearn.layer ...

  4. Springboot配置注入

    springboot中如何将yml 配置文件中配置,在类中注入使用 ①第一种使用@value方式 ################## 配置文件 ######################### d ...

  5. Linux从一台linux机器复制文件到另一台linux机器

    1.功能说明 scp 用于将文件/目录从一台linux系统复制到另一台linux系统.传输协议为SSH协议,保证了传输数据的安全性 其格式如下: (1)scp  本地linux系统文件路径   远程用 ...

  6. vim 复制 单个 单词: 移动光标到单词词首,快速摁 yw

    vim 复制 单个 单词:   移动光标到单词词首,快速摁 yw

  7. Nginx_安全2

    Nginx与安全有关的配置 隐藏版本号 http {    server_tokens off;} 经常会有针对某个版本的nginx安全漏洞出现,隐藏nginx版本号就成了主要的安全优化手段之一,当然 ...

  8. 39)PHP,选取数据库中的两列

    首先是我的文件关系: 我的b.php是主php文件,BBB.php是配置文件,login.html是显示文件, b.php文件代码: <?php /** * Created by PhpStor ...

  9. Spring Cloud服务消费者(rest+ribbon)

    在上一篇文章,讲了服务的注册和发现.在微服务架构中,业务都会被拆分成一个独立的服务,服务与服务的通讯是基于http restful的.Spring cloud有两种服务调用方式,一种是ribbon+r ...

  10. java 解析URL里的主域名及参数工具类

    java 解析URL里的协议及参数工具类,解析URL中的主域名,并统一把协议修改成http或去掉协议 public class UrlDomainUtils { private static fina ...