总结一下“入门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. PHP系列 | ThinkPHP5.1 如何自动加载第三方SDK(非composer包 )

    注意:这里只是针对于非Composer 安装包的自动加载的实现,能用composer安装的自动跳过. 由于ThinkPHP5.1 严格遵循PSR-4规范,不再建议手动导入类库文件,所以新版取消了Loa ...

  2. Activity组件(三):通过对象实现信息添加及展示

    在对组件进行注册时,只注册了EditText,却忘记了Button,导致程序一直闪退 输入信息 点击添加 成功跳转页面,并将数据传递 User.java package com.example.reg ...

  3. Laravel常见问题总结

    1.Whoops, looks like something went wrong. 一般报这个问题是由于复制框架文件时没有把相应的env (隐藏文件) 复制 导致新复制的框架没有配置选项 解决方法: ...

  4. 2019-2020-1 20199324《Linux内核原理与分析》第九周作业

    第八章 进程的切换和系统的一般执行过程 1.进程调度的时机 硬中断和软中断 中断:在本质上都是软件或者硬件发生了某种情形而通知处理器的行为,处理器进而停止正在运行的指令流(当前进程),对这些通知做出相 ...

  5. c语言中对字段宽度的理解?

    /************************************************************************* > File Name: printf.c ...

  6. c语言中,为什么以下程序直接按”Enter“也就是回车程序不结束?

    /************************************************************************* > File Name: system.c ...

  7. ubuntu16.04更换 apt-get 阿里源

    deb-src http://archive.ubuntu.com/ubuntu xenial main restricted #Added by software-properties deb ht ...

  8. Web 自动化

    自动化:由机器设备代替人为自动完成指定目标的过程 自动化测试:由程序代替人为去验证程序功能的过程 为什么要进行自动化测试? 解决-回归测试 压力测试 兼容性测试 提高测试效率,保证产品质量 什么阶段开 ...

  9. 引力波的绘制(python)

    import numpy as np import matplotlib.pyplot as plt from scipy.io import wavfile rate_h,hstrain = wav ...

  10. 快手为什么要全资收购Acfun?

    近日据媒体报道,快手已完成对「Acfun」以下简称A站的全资收购.未来A站仍将保持独立品牌和独立运营以及原有团队的独立发展.近年来,A站可谓命途多舛.相比隔壁B站风风光光顺利上市且成为真正的二次元大本 ...