3

次の形式の約 2,000 万行のデータセットがあります。

Userid attributid timeid
1      -1         0
1      -2         0
1      -3         0
1      -4         0
1      -5         0
...

そして、attributeid を 4 つの属性タイプのいずれかに一致させる別のインデックス:

attributeid attributetype
-1          A
-2          B
-3          C
-4          D
-5          B

データセットを次の形式に変換して、neo4j に一括インポートしたいと思います。

UserID A     B      C     D     timeid
1      -1    -2,-5  -3    -4    0

ユーザーIDを使用してデータフレームを注文し、それをスキャンしてRを試しました。しかし、遅すぎました。それを行うための最も時間効率の良い方法は何だろうと思っていましたか?または、コードを最適化するためにできることはありますか? これが私のコードです:

names(node_attri)[1] = 'UserID'
names(node_attri)[2] = 'AttriID'
names(node_attri)[3] = 'TimeID'
names(attri_type)[1] = 'AttriID'
names(attri_type)[2] = 'AttriType'
#attri_type <- attri_type[order(attri_type),]
#node_attri <- node_attri[order(node_attri),]

N = length(unique(node_attri$TimeID))*length(unique(node_attri$UserID))
new_nodes = data.frame(UserID=rep(NA,N), employer=rep(NA,N), major=rep(NA,N), 
                  places_lived=rep(NA,N), school=rep(NA,N), TimeID=rep(NA,N))
row = 0
start = 1
end = 1
M =nrow(node_attri)  
while(start <= M) {
  row = row + 1
  em = ''
  ma = ''
  pl = ''
  sc = ''
  while(node_attri[start,1] == node_attri[end,1]) {
    if (attri_type[abs(node_attri[end,2]),2] == 'employer') 
       em = paste(em, node_attri[end,2], sep=',')
    else if (attri_type[abs(node_attri[end,2]),2] == 'major')
       ma = paste(ma, node_attri[end,2], sep=',')
    else if (attri_type[abs(node_attri[end,2]),2] == 'places_lived')
       pl = paste(pl, node_attri[end,2], sep=',')
    else if (attri_type[abs(node_attri[end,2]),2] == 'school')
      sc = paste(sc, node_attri[end,2], sep=',')
    end = end + 1
    if (end > M) break
  }
  new_nodes[row,] = list(UserID=node_attri[start,1], employer=substring(em,2), 
                       major=substring(ma,2), places_lived=substring(pl,2), 
                       school=substring(sc,2), TimeID=node_attri[start,3])
  start = end
  end = start
}
new_nodes = new_nodes[1:row,]
4

2 に答える 2