select()
function in R Language is used to choose whether a column of the data frame is selected or not.
Syntax: select(x, expr)
Parameters:
x: Data frame
expr: condition for selection
Example 1:
library(dplyr)
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" ) )
select(d, starts_with( "ht" ))
select(d, - starts_with( "ht" ))
|
Output:
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
Example 2:
library(dplyr)
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" ) )
select(d, 1 : 2 )
select(d, contains( "a" ))
select(d, matches( "na" ))
|
Output:
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
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
19 Jun, 2020
Like Article
Save Article