在另一个矩阵中找到列表/矩阵的相同观测值 [英] Find same observations of a list/matrix in another matrix

查看:118
本文介绍了在另一个矩阵中找到列表/矩阵的相同观测值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于营销类,我必须编写一个计算客户保留率(客户仍然是客户的概率)的函数.到目前为止,我已经隔离了各个客户的ID,并将它们存储在first.transactions.data矩阵中.然后,我使用split()将其分为同类群组(按时间划分的客户群),并将其存储在列表同类群组中. 现在出现了我的问题:我从完整数据集中计算了另一个子矩阵,称为final.period.data,在其中我将计算保留率.但是,因此我必须在每个队列的final.period.data中隔离ID.我的老师告诉我,我应该在final.period.data中创建一列,以显示TRUE或FALSE,这取决于同类群组的ID和final.period.data的ID是否相同.为此,我尝试使用存在,但始终收到错误消息.我尝试了以下方法:

For a marketing class I have to write a function that calculates the retention rate of the customers (probability that a customer still is a customer). I've come so far that I isolated the ids of the individual customers and stored them in the matrix first.transactions.data. I then split them into cohorts (group of customers by time) with split() and stored them in the list cohort. Now comes my problem: I calculated another sub-matrix from the full data set called final.period.data where I will calculate the retention rate. However, therefore I have to isolate the ids in final.period.data for each cohort. My instructor told me that I should create an additional column in final.period.data that shows TRUE or FALSE depending on whether the cohort's id and final.period.data's id are the same. For this I tried to use exists, but I always receive error messages. I tried the following:

final.period.data <- if(exists(cohort$'1'$id, where = final.period.data$id) final.period.data$same = TRUE)

,但始终会收到错误消息,例如:意外的符号或无效的第一个参数.我还尝试将列表队列转换为矩阵,但这也无济于事.我该如何更改exist命令,或者在final.period.data中有一种更简单的方法来查找同类群组的ID?

but always receive error messages such as: unexpected symbol or invalid first argument. I also tried to convert the list cohort into a matrix but this didn't help either. How do I have to change the exist command or is there a simpler way to locate cohort's ids in final.period.data?

谢谢您的帮助.

推荐答案

您可以创建一个可以实现所需功能的函数:

You can just create a function that does what you want:

funct <-(final.period.data){
 if (final.period.data$cohort =='1' & final.period.data$id ==<condition2>){
   #Change the number for the TRUE condition}
 else{ #If it doesn't fit the two conditions
  #Change the number for the FALSE condition}
}
vector <- c(nrow(final.period.data))
final.period.data <- cbind(vector)

并将其用作应用功能. 在这里,您会找到有关申请的更多信息 但是我通常使用for循环来执行此操作,首先创建新列,然后将其添加到数据框中.

And use it as the apply function. Here will you find more information about apply But I usually do it with a for loop, first creating the new column and then adding it to the data frame.

这篇关于在另一个矩阵中找到列表/矩阵的相同观测值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆