data.frame和matrix的一些操作】的更多相关文章

编写脚本的时候经常会涉及到对data.frame或matrix类型数据的操作,比如取指定列.取指定行.排除指定列或行.根据条件取满足条件的列或行等.在R中,这些操作都是可以通过简单的一条语句就能够实现的,非常的简洁优美~ 下面就是一个小小的总结: 1. 对data.frame或matrix取指定行 data[i,] #取data中的第i行 data[c(i,j,k),] #取data中的第i,j,k行 取指定列的操作同理可得 2. 对data.frame或matrix删除指定行 data[-i,…
Data Frame一般被翻译为数据框,感觉就像是R中的表,由行和列组成,与Matrix不同的是,每个列可以是不同的数据类型,而Matrix是必须相同的. Data Frame每一列有列名,每一行也可以指定行名.如果不指定行名,那么就是从1开始自增的Sequence来标识每一行. 初始化 使用data.frame函数就可以初始化一个Data Frame.比如我们要初始化一个student的Data Frame其中包含ID和Name还有Gender以及Birthdate,那么代码为: studen…
Data Frame一般被翻译为数据框,感觉就像是R中的表,由行和列组成,与Matrix不同的是,每个列可以是不同的数据类型,而Matrix是必须相同的. Data Frame每一列有列名,每一行也可以指定行名.如果不指定行名,那么就是从1开始自增的Sequence来标识每一行. 初始化 使用data.frame函数就可以初始化一个Data Frame.比如我们要初始化一个student的Data Frame其中包含ID和Name还有Gender以及Birthdate,那么代码为: studen…
################################################### 问题:生成.操作数据框   18.4.27 怎么生成数据框 data.frame.,,及其相关操作 ??? 解决方案: iris[,2,drop = FALSE]   #用drop参数,控制截取的子集所生成变量的格式,为单列的"数据框" dfm1 <- cbind(c1,c2,c3,c4);   #cbind 要求向量c1 c2 c3 c4长度必须一样. dfm2 <-…
a = matrix(     c(2, 4, 3, 1, 5, 7), # the data elements     nrow=2,              # number of rows     ncol=3,              # number of columns     byrow = TRUE)        # fill matrix by rows     class( (a[1,])[1] "numeric" class(a[c(1,2),])[1] &…
1 向一个data.frame指定列插入一列新数据 1.1 插入一列到指定位置 y<-1:4 data1 <-data.frame(x1=c(1,3,5,7), x2=c(2,4,6,8),x3=c(11,12,13,14),x4=c(15,16,17,18)) data2<-cbind(data1[,1:2],y,data1[,3:ncol(data1)]) 插到data1末尾 data2<-cbind(data1,y) 插到第一列 data2<-cbind(y,data1…
A data frame is used for storing data tables. It is a list of vectors of equal length. For example, the following variable df is a data frame containing three vectors n, s, b. > n = c(2, 3, 5) > s = c("aa", "bb", "cc") …
替换空值: foo <- data.frame("day"= c(1, 3, 5, 7), "od" = c(0.1, "#N/A", 0.4, 0.8)) NAs <- foo == "#N/A" ## by replace method is.na(foo)[NAs] <- TRUE ## or directly foo[NAs] <- NA 替换负值为0: 方法一: df[df < 0] =…
例如将如下数据转换成data.frame型: l <- replicate( 5, list(sample(letters, 4)), simplify = FALSE ) => 用unlist拆分list后重构矩阵然后转换为data.frame lr <- data.frame(matrix(unlist(l), nrow=5, byrow=T),stringsAsFactors=FALSE) lapply 函数和sapply函数 data.frame(matrix(lapply(l,…
一.Python 数据框就是典型的关系型数据库的数据存储形式,每一行是一条记录,每一列是一个属性,最终构成表格的形式,这是数据科学家必须熟悉的最典型的数据结构. 1.构建数据框 import pandas as pd data = {'year':[2010, 2011, 2012, 2010, 2011, 2012, 2010, 2011, 2012], 'team':['FCBarcelona', 'FCBarcelona', 'FCBarcelona', 'RMadrid', 'RMadr…
https://www.datamentor.io/r-programming/data-frame/ Check if a variable is a data frame or not We can check if a variable is a data frame or not using the class() function. > x SN Age Name 1 1 21 John 2 2 15 Dora > typeof(x) # data frame is a specia…
使用 R包 xlsx 或者 openxlsx 安装 install.packages("xlsx", repos="https://cloud.r-project.org/") install.packages("openxlsx", repos="https://cloud.r-project.org/") 使用 文件名+sheet的序号读取指定sheet的内容 data <- read.xlsx("Lips…
问题: 在ArcMap中,菜单Insert下Data Frame,可以在TOC中增加Data Frame,在MapControl或者PageLayoutControl下都可以正常显示多个Data Frame,并且这些Data Frame可以方便切换, 但是在AE开发中,MapControl无法实现此效果,需要特殊控制(如何控制?)还是无法实现? 易智瑞(中国)信息技术有限公司客户与合作伙伴支持部 石羽  回复: ArcMap中Toc窗体实际仅和Pagelayout窗体进行绑定,切换窗体时并没有切…
x<-data.frame(apple=c(1,4,2,3),pear=c(4,8,5,2)) x # apple pear # 1 1 4 # 2 4 8 # 3 2 5 # 4 3 2 x$banana<-c(9,5,6,2) x # apple pear banana # 1 1 4 9 # 2 4 8 5 # 3 2 5 6 # 4 3 2 2…
data.frame类型数据如何将第一列值替换为行号 row.names(data) <- data[, 1]data <- data[, -1]…
在使用ARIMA模型来预测我们的销量的时候,如果保存预测版本进DB,以供后续分析呢 1. 在定义变量阶段我们定义了dfResult      <- data.frame() 这是一个data frame 2. 预测的结果通常是一个vector 例如我们预测90个期间的数据:pred <- predict(fit, n.ahead = 1*90) 3. 假设我们每天都保存一次预测版本,可以设置变量如下 strPredictVer <- format(Sys.Date(),"%Y-…
将R非时间序列的data.frame转变为时序格式,常常会用到,尤其是股票数据处理中, 举例:dailyData包括两列数据:Date Close10/11/2013 871.9910/10/2013 868.2410/9/2013 855.8610/8/2013 853.6710/7/2013 865.7410/4/2013 872.3510/3/2013 876.0910/2/2013 887.9910/1/2013 8879/30/2013 875.919/27/2013 876.399/…
Say I have a data.frame: df <- data.frame(A=c(10,20,30),B=c(11,22,33), C=c(111,222,333))  A  B  C1 10 11 1112 20 22 2223 30 33 333 If I select two (or more) columns I get a data.frame: x <- df[,1:2]   A  B 1 10 11 2 20 22 3 30 33 This is what I want…
Problem You want to do reorder the columns in a data frame. Solution # A sample data frame data <- read.table(header=TRUE, text=' id weight size 1 20 small 2 27 large 3 24 medium ') # Reorder by column number data[c(1,3,2)] #> id size weight #> 1…
You should use either indexing or the subset function. For example : R> df <- data.frame(x=1:5, y=2:6, z=3:7, u=4:8) R> df x y z u 1 1 2 3 4 2 2 3 4 5 3 3 4 5 6 4 4 5 6 7 5 5 6 7 8 Then you can use the which function and the - operator in column…
按照某列的值拆分data.frame My data is like this (for example): ID Rate State 1 24 AL 2 35 MN 3 46 FL 4 34 AL 5 78 MN 6 99 FL I want to split the data by state and I want to get 3 data sets like below: data set 1 ID Rate State 1 24 AL 4 34 AL data set 2 ID Ra…
Merging Data Adding Columns To merge two data frames (datasets) horizontally,  use the merge function. In most cases, you join two data frames  by one or more common key variables (i.e., an inner join). # merge two data frames by ID   total <- merge(…
df<-NULL new_row<-data.frame(colA="xxx",colB=123) df<-rbind(df,new_row)…
################################################### 问题:数据框 data.frame 查.排序等,   18.4.27 怎么对数据框 data.frame实施 查询位置.查询满足条件的个案数..排序. ??? 解决方案: #查询位置 weizhi <- which(iris$Sepal.Length >= 6.9)   #返回一个向量,显示的是所有 >=6.9的行的行号 max(iris$Sepal.Length)    #[1] 7…
面对的是这样一个问题,不断读入一行一行数据,append到data frame上,如果用dataframe,  rbind() ,可以发现数据大的时候效率明显变低. 原因是 每次bind 都是一次重新整个数据集的重新拷贝 这个链接有人测试了各种方案,似乎给出了最优方案 http://stackoverflow.com/questions/11486369/growing-a-data-frame-in-a-memory-efficient-manner library(data.table) d…
data.frame比较像表格,每一列是一个向量,即每列中的元素是同一类型:所有列具有相同的长度. x = 10:1 y = -4:5 q = c("Ha","oh","wow","yes","Noooo","wasami","cuckoo","sweet","candy","gloss") df1 = da…
假设说一个data.frame中的元素是factor.你想转化成numeric,你会怎么做?比方d[1,1]是factor   正确答案是 先as.character(x) 再as.numeric(x)   哈哈,我刚发现假设直接as.numeric,就不是曾经的数字了,坑爹啊.   原来as.data.frame()有一个參数stringsAsFactors   假设stringAsFactor=F   就不会把字符转换为factor 这样以来.原来看起来是数字变成了character,原来是…
pathway_name = rownames(g1) tm <- list('P-value' = c(), 'Pathway_name' = c()) :dim(g1)[]){ results<- t.test(g1[i,],g2[i,])$p.value print(results) tm$`P-value`<-append(tm$`P-value`,results) tm$Pathway_name<- append(tm$Pathway_name,pathway_name[…
课程介绍:Data science is a "concept to unify statistics, data analysis, machine learning and their related methods" in order to "understand and analyze actual phenomena" with data1. With the development of the technologies of data collecti…