【发布时间】:2021-10-26 12:22:33
【问题描述】:
我以前使用 R 进行数据处理,现在我的新项目需要 Java,所以如果我提出幼稚的问题,我深表歉意。我的问题是我想在 R dplyr 中实现类似 filter 的东西。基本上,我现在有两个 CSV 文件:
一个是关于person属性,其中每一行的第一个元素代表一个唯一的personID:
1,4CC0D97F9ECC6B1A,MUTSAARD,7,m,7-8,0,0,ACT
2,F6B73020FC552E32,PORTE TERVUEREN,3,m,3-4,3,0,EMP
4,4072878C4683C96F,ALTITUDE 100,4,f,1-2,5,1,EMP
另一个 CSV 是关于人员活动,其中每行的第一个元素也代表唯一的 personID:
1,0,0,0,9,34,home,34200,150101.5,176176
1,1,10,34,13,34,leisure,48600,249319.227415549,64034.2890971927
1,2,14,34,14,35,home,600,249319.227415549,64034.2890971927
1,3,15,49,16,19,shopping,58800,281683.200856897,118126.130836235
1,4,18,4,25,0,home,90000,281683.200856897,118126.130836235
2,0,0,0,15,38,home,56400,152056.679999997,170502.339842428
2,1,15,48,24,1,work,86400,153720.999999996,167515.000842442
2,2,24,18,25,0,home,90000,156685.535763012,169194.702448164
4,0,0,0,9,58,home,36000,147758.618000003,167097.459842441
4,1,10,29,14,58,work,54000,147251.000000004,174872.000842412
4,2,15,28,16,28,shopping,59400,144431.419000006,166735.039842444
4,3,16,38,18,38,leisure,67200,146053.041238428,169647.999589575
4,4,18,58,25,0,home,90000,149907.09447342,170229.096090939
我现在要做的是首先循环 person 属性并在那里进行一些编码,之后,我想过滤活动 CSV 中具有相同 personID 的行并循环具有相同 personID 的那些行并做一些在那里编码。
所以我现在的代码是:
BufferedReader attributeReader = new BufferedReader(new FileReader(attributesFile));
String agent = null;
while ((agent = attributeReader.readLine()) != null) {
String[] attributeSpilted = agent.split(",");
int attributeAgentID = Integer.parseInt(attributeSpilted[0]);
// Set attributes for agents
Person person = populationFactory.createPerson(Id.createPersonId(attributeAgentID));
// Question: What I should do here to find the activities with the same personID?
population.addPerson(person);
}
我的问题是在代码中,我卡在那里并且不确定我应该在这里做什么才能找到具有相同 personID 的活动?
【问题讨论】:
-
将两个 CSV 文件视为具有基于
attributeAgentID的关系,以便第二个 CSV 中所需的信息位于具有相同attributeAgentID的行上。有不同的方法可以做到这一点,首先尝试最简单和最慢的方法,以确保您的关系正常。
标签: java