zoukankan      html  css  js  c++  java
  • R: data.frame 数据框的:查询位置、排序(sort、order)、筛选满足条件的子集。。

    ###################################################

    问题:数据框 data.frame 查、排序等,   18.4.27

             怎么对数据框 data.frame实施 查询位置、查询满足条件的个案数、、排序、 ???

    解决方案:

             #查询位置

             weizhi <- which(iris$Sepal.Length >= 6.9)   #返回一个向量,显示的是所有 >=6.9的行的行号

             max(iris$Sepal.Length)    #[1] 7.9

             which(iris$Sepal.Length == max(iris$Sepal.Length))   #[1] 132

             which(iris$Species == "setosa" & iris$Sepal.Length >= 5.1)   #which 返回满足其内条件的元素的位置

             x[x==3] <- 25;x    #将x中等于3的元素值变为25

             x[x=1] <- 12;x    #将x的第1个元素数值变为12

             which.max(a)    # 找出最大元素的下标

             which.min(a)    # 找出大小的元素下标

             which(a==2)    # 等于2的元素的下标

             which(a>5)     # 大于5的元素的下标

             a[which.max(a)]    # 找出最大的元素

             a[which(a>5)]    # 所有的值大于5的元素

             #查询满足条件的个案数

             length(which(iris$Species == "setosa" & iris$Sepal.Length >= 5.1))      

     sum(iris$Sepal.Length >= 6)    #统计个数,统计 iris$Sepal.Length 中大于等于6的数有多少个

             #排序

             #order();   #输出原行号,按照要求排序后的向量  order返回的是排序后的索引。

             order(iris$Sepal.Length)    #[1]  9  4  7  3  2 10  5  8  1  6 ;返回原来的行号按照iris$Sepal.Length排序后的向量

             inew = iris[order(iris$Sepal.Length),]   #由于要对行排序,所以条件要写在 中括号内 行的位置。

             inew2 = iris[order(iris$Sepal.Length,-iris2$Sepal.Width),]  #默认升序  本例按iris$Sepal.Length升序,按iris$Sepal.Width降序。

             #sort();   #直接输出排序后的结果。

    讨论扩展:

    另请参阅:

    ###################################################

    问题:筛选子集(行子集)   18.4.24

             根据 data.frame 中某列,如何筛选满足条件的行,组成一个子集

    解决方案:

             cond <-  iris$Species == "setosa" & iris$Sepal.Length >= 5.1   #筛选条件,返回一个与 iris$Species 长度相同的布尔向量。满足条件的为TRUE。

                       # 和&  或|  不等于!=  大于>  小于< ,自由组合      #如果筛选对象是因子变量,需要带引号    == “”

             筛选子集 法一用 subset 选子集

             iris_setosa1 <- subset(iris,cond);

             iris_setosa1 <- subset(iris,cond,select = c(1:3));   #select 用来选择 “只取select内的列” 。

             iris.del.col2 <- subset(iris,cond,select = -Sepal.Width)

             iris.del.col2 <- subset(iris,cond,select = c(-Sepal.Width,-Petal.Width))    #以下均可:select = c(Sepal.Width,Petal.Width)  select = c(Sepal.Width,5)

             筛选子集 法二

             iris_setosa2 <- iris[cond,];

             newdata <- iris[which(cond),c(1,2,5)]   #选择满足条件的行和列,一并生成新变量

    讨论扩展:

             对列筛选:

             subset(dfrm,select=c(colname1,colname2,...,colnames),subset = (temp));   #同时实现对行、列的筛选。。内部subset对行进行筛选。

             student[which(student$Gender==”F”),”Age”]     #筛选后,只取“Age列”,条件得到一个布尔向量:FALSE FALSE  TRUE,然后使用which函数可以将布尔向量中TRUE的Index返回,

               iris.Species2 = iris[iris$Species == "setosa",]    #等价,更常用。和& 或| 不等于!= 大于> 小于< ,自由组合

                       #如果筛选对象是因子变量,需要带引号 == “”

             newdata <- iris[which(iris$Species =='setosa' & iris$Sepal.Length > 5.0),c(1,2,5)]

             iris_6 <- subset(iris,iris$Sepal.Length >6 | iris$Sepal.Width>4,select = c(1:3))

    另请参阅:

  • 相关阅读:
    返回一个整数数组中最大子数组的和2
    RT-Thread之自动初始化
    Git
    基于STM32的FreeRTOS移植
    RT-Thread之debug使用
    大数的进制转换
    uva-10110
    UVA-10061
    算法训练Maze
    森林变树
  • 原文地址:https://www.cnblogs.com/li-20151130/p/9028752.html
Copyright © 2011-2022 走看看