select()
R語言中的函數用於選擇是否選擇 DataFrame 的列。
用法: select(x, expr)
參數:
x: DataFrame
expr:選擇條件
範例1:
# R program to select specific columns
# 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", "yes", "no", "no") )
# startswith() function to print only ht data
select(d, starts_with("ht"))
# -startswith() function to
# print everything except ht data
select(d, -starts_with("ht"))
輸出:
ht 1 46 2 NA 3 NA 4 69 name age school 1 Abhi 7 yes 2 Bhavesh 5 yes 3 Chaman 9 no 4 Dimri 16 no
範例2:
# R program to select specific columns
# 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", "yes", "no", "no") )
# Printing column 1 to 2
select(d, 1:2)
# Printing data of column heading containing 'a'
select(d, contains("a"))
# Printing data of column heading which matches 'na'
select(d, matches("na"))
輸出:
name age 1 Abhi 7 2 Bhavesh 5 3 Chaman 9 4 Dimri 16 name age 1 Abhi 7 2 Bhavesh 5 3 Chaman 9 4 Dimri 16 name 1 Abhi 2 Bhavesh 3 Chaman 4 Dimri
相關用法
- R語言 rename()用法及代碼示例
- R語言 data.matrix()用法及代碼示例
- R語言 with()用法及代碼示例
- R語言 expand.grid()用法及代碼示例
- R語言 subset()用法及代碼示例
- R語言 melt()用法及代碼示例
- R語言 mutate()用法及代碼示例
- R語言 summarise()用法及代碼示例
- R語言 sample_n()用法及代碼示例
- R語言 merge()用法及代碼示例
- R語言 choose()用法及代碼示例
- R語言 transform()用法及代碼示例
注:本文由純淨天空篩選整理自nidhi_biet大神的英文原創作品 Choose Specific Columns of a Data Frame in R Programming – select() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。