-2

i have a df as below: where there are 2 columns, student names and marks.

Stud_name   Marks
Jon         25
john        20
ajay        50
ram         27
jay         61
jess        46
troy        23
mike        42
steve       45
glenn       43

i want few name and their marks.

expected output:

Stud_name   Marks
john        20
ajay        50
jess        46
troy        23
ram         27
glenn       43

please help.

i tried:

pd <- filter(df,Stud_name == "john" , "ajay" , "jess")

Error in filter_impl(.df, quo) : 
Evaluation error: operations are possible only for numeric, logical or 
    complex types.
Robby star
  • 281
  • 2
  • 13

1 Answers1

2

You can try this, if you can think to use a base solution:

# your data
dats <- read.table(text='Stud_name   Marks
Jon         25
                   john        20
                   ajay        50
                   ram         27
                   jay         61
                   jess        46
                   troy        23
                   mike        42
                   steve       45
                   glenn       43',sep='', header=T)

# vector with choosen names
names <- c("john","ajay","jess")
dats[which(dats$Stud_name %in% names),]

or (thanks @markus):

dats[(dats$Stud_name %in% names),]
  Stud_name Marks
2      john    20
3      ajay    50
6      jess    46
s__
  • 9,270
  • 3
  • 27
  • 45