Blue_Lion
Blue_Lion

Reputation: 73

creating a function to subset data frame in R

I am trying to write a function in R to subset data based on ID in a data frame like below, but the function I wrote does not work and it doesn't give me an error message either. Can someone let me know how to fix my function code?

subset1<-function(id){df2<-df[df$ID==id,]}

enter image description here

Upvotes: 2

Views: 2227

Answers (1)

akrun
akrun

Reputation: 887901

We can use [[ inside a function

f1 <- function(id){
      df[df[["ID"]] == id,]
    }
f1(11)
#  ID Item
#1 11    a

Upvotes: 3

Related Questions