array, matrix, list and dataframe
总结一下“入门3R”(Reading, ‘Riting, ‘Rrithmetic)中的读和写,不同的数据结构下的读写还是有点区别的。
vector
命名
1 |
month.days<-c(31,28,31,30,31,30,31,31,30,31,30,31) |
操作文本
1.文本分离
1 |
pangram<-"The quick brown fox jumps over the lazy dog" |
strplit()函数将pangram用空格切开,这个函数的返回值是list
1 |
words<-strsplit(pangram," ")[[1]] |
可以取出字符串数组
2.文本连接
1 |
paste(LETTERS[1:5],1:5,sep="_",collapse="---") |
用空格连接words中的元素,paste()接收的参数应该是多个变量,sep决定多个向量之间的连接符,而collapse决定统一向量中的元素怎么合并。
3.文本排序
1 |
sort(letters,decreasing=TRUE) |
4.查找文本
1 |
substr(state.name,start=3,stop=6) |
grep(pattern,x)返回的是符合pattern的元素的在x中的位置
5.文本替换
1 |
gsub("cheap","sheep's","A wolf in cheap clothing")
|
1 |
x<-c("file_a.csv","file_b.csv","file_c.csv")
|
因子分类
factor(x,levels,labels)可以创建R因子,而levels指的是x的输入值,labels表示新创建的因子的输出值。
因子转换
1 |
numbers<-factor(c(9,8,10,8,9)) |
有序因子
类别数据的统计
1 |
table(state.region) |
有序变量
- 使用
factor()函数,并且指定参数ordered=TRUE - 使用
ordered()函数
matrix
1 |
matrix(data,ncol,nrow,byrow) |
索引、修改和命名
1 |
first.matrix<-matrix[1:12,ncol=4,byrow=TRUE] |
计算
1 |
t()###转置 |
array
向量和矩阵都是数组.
1 |
array(1:24,dim=c(3,4,2))###创建一个三维数组 |
data.frame
由矩阵创建 x.df<-as.data.frame(x)
由向量创建 data<-data.frame(x,y,z)
如果创建的变量是字符串类型,R会自动转换成因子,可以用stringAsFactor=FALSE保持字符串类型
1 |
names(data)[2]<-'B' ###命名表头 |
操作data.frame中的值
data.frame并不是向量,而是一组向量列表。但是数据操作时可以当做矩阵来处理,访问单个变量时可以用$,访问多个变量时可以用[]
1 |
#########修改值 |
list
1 |
#######创建list |
提取列表中的元素
- 使用
[[]]返回元素本身 - 使用
[]返回选定元素的列表
1 |
#########修改元素值 |
array, matrix, list and dataframe的更多相关文章
- numpy中list array matrix比较
用python中的numpy包的时候不小心踩了array和matrix的大坑,又引申一下比较list array matrix之间的异同.数据结构(Data Structures)基本上人如其名——它 ...
- array / matrix subarray/submatrix sum
Maximal Subarray Sum : O(n) scan-and-update dynamic programming, https://en.wikipedia.org/wiki/Maxim ...
- 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 ...
- C#+无unsafe的非托管大数组(large unmanaged array in c# without 'unsafe' keyword)
C#+无unsafe的非托管大数组(large unmanaged array in c# without 'unsafe' keyword) +BIT祝威+悄悄在此留下版了个权的信息说: C#申请一 ...
- C++_Eigen函数库用法笔记——The Array class and Coefficient-wise operations
The advantages of Array Addition and subtraction Array multiplication abs() & sqrt() Converting ...
- 【LeetCode】74. Search a 2D Matrix
Difficulty:medium More:[目录]LeetCode Java实现 Description Write an efficient algorithm that searches f ...
- Spark MLlib 之 大规模数据集的相似度计算原理探索
无论是ICF基于物品的协同过滤.UCF基于用户的协同过滤.基于内容的推荐,最基本的环节都是计算相似度.如果样本特征维度很高或者<user, item, score>的维度很大,都会导致无法 ...
- pandas 数据结构基础与转换
pandas 最常用的三种基本数据结构: 1.dataFrame: https://pandas.pydata.org/pandas-docs/stable/generated/pandas.Data ...
- pandas 之 特征工程
import numpy as np import pandas as pd So far(到目前为止) in this chapter we've been concerned with rearr ...
随机推荐
- psi 函数计算
scipy.special.psi odps中不支持 scipy.special.psi,需要改写 基于 chebyshev_polynomial https://people.sc.fsu.edu/ ...
- 吴裕雄--天生自然 pythonTensorFlow图形数据处理:windows操作系统删除tensorflow
输入:pip uninstall tensorflow Proceed(y/n):y
- PAT甲级——1005.SpellItRight(20分)
Given a non-negative integer N, your task is to compute the sum of all the digits of N, and output e ...
- 【转】浅述WinForm多线程编程与Control.Invoke的应用
环境:VS2008.C#3.0 在WinForm开发中,我们通常不希望当窗体上点了某个按钮执行某个业务的时候,窗体就被卡死了,直到该业务执行完毕后才缓过来.一个最直接的方法便是使用多线程.多线程编程的 ...
- 893B. Beautiful Divisors#美丽的因子(打表法)
题目出处:http://codeforces.com/problemset/problem/893/B 题目大意:找到一个数在二进制下,最大的以k个连续的1和k-1个连续的0组成的数字作为因子 #in ...
- 【Java杂货铺】JVM#Java高墙之内存模型
Java与C++之间有一堵由内存动态分配和垃圾回收技术所围成的"高墙",墙外的人想进去,墙外的人想出来.--<深入理解Java虚拟机> 前言 <深入理解Java虚 ...
- vue中v-on支持的事件总结
资源事件 事件名称 何时触发 error 资源加载失败时. abort 正在加载资源已经被中止时. load 资源及其相关资源已完成加载. beforeunload window,document 及 ...
- LGOJ3327 【SDOI2015】约数个数和
又是一道卡常好题 坑掉我的 \(define \space int \space long \space long\) 感觉出题人并没有获得什么快乐-- Description link 题意概述: ...
- linux重定向与管道符(一)
linux重定向和管道符 为什么要使用重定向 1.当屏幕输出的信息很重要,而且我们需要将他存下来的时候: 2.后台执行中的程序,不希望他干扰屏幕正常的输出结果时: 3.系统的例行命令,例如定时任务的执 ...
- redis安装zmalloc.h:50:31: 致命错误:jemalloc/jemalloc.h:没有那个文件或目录
问题: zmalloc.h:50:31: 致命错误:jemalloc/jemalloc.h:没有那个文件或目录 解决方案:执行命令:make MALLOC=libc make MALLOC=libc