使用参数在用户定义的函数中定义提取运算符

时间:2019-04-09 21:54:17

标签: r

我创建了一个使用子集函数的函数。如何分配参数,然后在提取运算符之后使用它?

这是我现在拥有的:

int main(int argc, char *argv[]) { ThreadData threadData; //the struct which is used as input in the // thread_transaction method, contains the id and other thread //information pthread_t* thread_ids = (pthread_t*)malloc(sizeof(pthread_t) * n_cust); for(i = 0; i < n_cust; i++){ threadData.thread_id = i; pthread_create(&thread_ids[i], NULL, &thread_transaction, (void *) (&threadData)); } for(j=0; j < n_cust; j++) { pthread_join(thread_ids[j], NULL); } printf("SEAT ARRANGEMENT: \n"); for(y=0; y<Nseat;y++){ printf("Seat %d / Costumer %d, ", y, threadData.seats_array[y]); } free(thread_ids); return 0; } void* thread_transaction(void* arg) { ThreadData* threadData = (ThreadData*) arg; pthread_mutex_lock(&id_mut); //i even tried mutex-locking the ID int id= threadData->thread_id; pthread_mutex_unlock(&id_mut); . . . printf("Your reservation is completed successfully. Your transaction number is %d ", id); //for 5 customers eg. it printed 4 0 4 2 2 }

给出以下呼叫: function_test<-function(time1,size,Param){ test1_in_equilibrium<-(subset(alldata,Time>=time1 & FinalPopSize==size)$Param) } 我希望R像这样扩展它: function_test(100,5000,Time)

不幸的是,当我尝试运行此功能时,收到错误对象“时间”。

我认为我缺少转义字符或类似字符,但是找不到它。

谢谢!

1 个答案:

答案 0 :(得分:2)

不能将$运算符添加到函数调用中,也不能将变量与$运算符一起使用。

但是,我了解到您想从子集data.frame中获得由输入变量Param定义的列。在这种情况下,您可以轻松编写如下函数:

function_test <- function(time1,size,Param){
  reduced_data <- subset(alldata, Time>=time1 & FinalPopSize==size)
  test1_in_equilibrium <- reduced_data[, Param]
}