filter()
R語言中的函數用於選擇案例並根據過濾表達式過濾掉值。
用法: filter(x, expr)
參數:
x:要過濾的對象
expr:作為過濾基礎的表達式
範例1:
# R Program to filter cases
# Loading library
library(dplyr)
# Create a data frame with missing data
d <- data.frame( name = c("Abhi", "Bhavesh", "Chaman", "Dimri"),
age = c(7, 5, 9, 16),
ht = c(46, NA, NA, 69),
school = c("yes", "yes", "no", "no") )
d
# Finding rows with NA value
filter(d, is.na(ht))
# Finding rows with no NA value
filter(d, ! is.na(ht))
輸出:
name age ht school 1 Abhi 7 46 yes 2 Bhavesh 5 NA yes 3 Chaman 9 NA no 4 Dimri 16 69 no name age ht school 1 Bhavesh 5 NA yes 2 Chaman 9 NA no name age ht school 1 Abhi 7 46 yes 2 Dimri 16 69 no
範例2:
# R Program to filter cases
# Loading library
library(dplyr)
# Create a data frame
d <- data.frame( name = c("Abhi", "Bhavesh", "Chaman", "Dimri"),
age = c(7, 5, 9, 16),
ht = c(46, NA, NA, 69),
school = c("yes", "no", "yes", "no") )
d
# Finding rows with school
filter(d, school == "yes")
# Finding rows with no school
filter(d, school == "no")
輸出:
name age ht school 1 Abhi 7 46 yes 2 Bhavesh 5 NA no 3 Chaman 9 NA yes 4 Dimri 16 69 no name age ht school 1 Abhi 7 46 yes 2 Chaman 9 NA yes name age ht school 1 Bhavesh 5 NA no 2 Dimri 16 69 no
相關用法
- R語言 complete.cases()用法及代碼示例
- R語言 cat()用法及代碼示例
- R語言 sapply()用法及代碼示例
- R語言 identity()用法及代碼示例
- R語言 type.convert()用法及代碼示例
- R語言 which()用法及代碼示例
- R語言 call()用法及代碼示例
- R語言 cumprod()用法及代碼示例
- R語言 is.character()用法及代碼示例
- R語言 ncol()用法及代碼示例
- R語言 is.factor()用法及代碼示例
- R語言 nrow()用法及代碼示例
- R語言 unique()用法及代碼示例
- R語言 max()用法及代碼示例
- R語言 min()用法及代碼示例
- R語言 str()用法及代碼示例
- R語言 cumsum()用法及代碼示例
- R語言 get()用法及代碼示例
- R語言 order()用法及代碼示例
- R語言 rowMeans()用法及代碼示例
- R語言 names()用法及代碼示例
- R語言 as.list()用法及代碼示例
注:本文由純淨天空篩選整理自nidhi_biet大神的英文原創作品 Filter Out the Cases from an Object in R Programming – filter() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。