简体   繁体   English

创建一个函数来对R中的数据帧进行子集

[英]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. 我正在尝试在R中根据以下数据帧中的ID向子集数据编写一个函数,但是我编写的函数不起作用,并且也不会给我任何错误消息。 Can someone let me know how to fix my function code? 有人可以让我知道如何修复我的功能代码吗?

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

enter image description here 在此处输入图片说明

We can use [[ inside a function 我们可以在函数中使用[[

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM